Hallo sehr geehrte Community Member,
ich habe ein Problem bei meiner App. Auf der Mainactivity habe ich ein DrawerLayout implementiert. So weit so gut, funktioniert, öffnet mit den Buttons eine neue Activity.
In dieser neu geöffneten Activity, besteht nun die Möglichkeit ein neues Objekt in der RecyclerView anzulegen. Die Daten greife ich aus einem Dialog ab, welcher die Klasse AppCompatDialogFragment extendet.
Daten abgreifen, erstellen des Objektes in der Datenbank ist soweit kein Problem. Klicke ich jedoch auf "Ok" beim Dialog, um die Daten zu speichern, schließt sich der Dialog UND ich werde auf die Mainactivity
zurück geschickt. Ich möchte jedoch nur, dass ich auf der aktuellen Activity bleibe und die RecyclerView aktualisiere.
Jemand eine Idee warum dies passiert?
=====================================
public class Unit_Activity extends AppCompatActivity implements Unit_Dialog_Add.Unit_Dialog_Listener {
private Unit_Adapter adapter;
FloatingActionButton buttonAddUnit;
Animation rotateClose, rotateOpen;
boolean isOpen = false;
private String userId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_unit);
initAddUnit();
setUpRecyclerView();
}
private void setUpRecyclerView(){
Query query = callRef.orderBy("unit_name",Query.Direction.DESCENDING);
FirestoreRecyclerOptions<Unit_Model> options = new FirestoreRecyclerOptions.Builder<Unit_Model>()
.setQuery(query, Unit_Model.class )
.build();
adapter = new Unit_Adapter(options);
RecyclerView recyclerView = findViewById(R.id.unit_recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
switch (direction){
case ItemTouchHelper.LEFT:{
adapter.deleteUnit(viewHolder.getAdapterPosition());
break;
}
case ItemTouchHelper.RIGHT:{
// edit
break;
}
}
}
@Override
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
new RecyclerViewSwipeDecorator.Builder(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
.addSwipeLeftBackgroundColor(ContextCompat.getColor(Unit_Activity.this, R.color.colorDelete))
.addSwipeLeftActionIcon(R.drawable.ic_delete)
.addSwipeRightBackgroundColor(ContextCompat.getColor(Unit_Activity.this, R.color.colorEdit))
.addSwipeRightActionIcon(R.drawable.ic_baseline_edit_24)
.create()
.decorate();
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
}).attachToRecyclerView(recyclerView);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener(){
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy){
if (dy > 0)
buttonAddUnit.hide();
else if (dy < 0)
buttonAddUnit.show();
}
});
}
// app im Vordergrund
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
// app im Hintergrund
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
public void openDialog(){
Unit_Dialog_Add unit_dialog_add = new Unit_Dialog_Add();
unit_dialog_add.show(getSupportFragmentManager(), "unit dialog");
}
private void initAddUnit() {
buttonAddUnit = findViewById(R.id.button_add_unit);
rotateClose = AnimationUtils.loadAnimation(this, R.anim.rotate_close_anim);
rotateOpen = AnimationUtils.loadAnimation(this, R.anim.rotate_open_anim);
buttonAddUnit.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button_add_unit:{
animateAddUnit();
openDialog();
break;
}
}
}
});
}
private void animateAddUnit(){
if (isOpen){
buttonAddUnit.startAnimation(rotateClose);
isOpen = false;
} else {
buttonAddUnit.startAnimation(rotateOpen);
isOpen = true;
}
}
@Override
public void applyInfos(String unit_name, String unit_location, String unit_callname) {
unitRef.add(new Unit_Model(1, unit_name, unit_location, unit_callname));
Toast.makeText(this,"Einheit erstellt!", Toast.LENGTH_SHORT).show();
finish();
animateAddUnit();
}
@Override
public void changeButton() {
animateAddUnit();
}
Alles anzeigen
public class Unit_Dialog_Add extends AppCompatDialogFragment {
private EditText edit_unit_name;
private EditText edit_unit_location;
private EditText edit_unit_district;
private Unit_Dialog_Add.Unit_Dialog_Listener unit_dialog_listener;
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_unit_add, null);
builder.setView(view)
.setTitle("Fahrzeugdaten")
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
unit_dialog_listener.changeButton();
}
})
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String unit_name = edit_unit_name.getText().toString();
String unit_location = edit_unit_location.getText().toString();
String unit_district = edit_unit_district.getText().toString();
unit_dialog_listener.applyInfos(unit_name, unit_location, unit_district);
}
});
edit_unit_name = view.findViewById(R.id.edit_unit_name);
edit_unit_location = view.findViewById(R.id.edit_unit_location);
edit_unit_district = view.findViewById(R.id.edit_unit_district);
return builder.create();
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
try {
unit_dialog_listener = (Unit_Dialog_Add.Unit_Dialog_Listener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement Vehicle_Dialog_Listener");
}
}
public interface Unit_Dialog_Listener{
void applyInfos(String unit_name, String unit_location, String unit_district);
void changeButton();
}
Alles anzeigen