3

I'm trying to perform an action in one fragment, then move to the previous fragment and show a snackbar with a message, confirming the action from the first fragment. However, I'm creating and showing the snackbar in the first fragment (the one I'm moving from), and the snackbar does not appear in the fragment I'm changing to, probably because it's shown in the fragment I'm moving from.

I'm executing the code inside an alertdialog:

builder.setPositiveButton(positiveText, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dbHandler.deleteExercise(exercise.getId());

                    // Making the snackbar here did not work, either.

                    getFragmentManager().popBackStack();

                    Snackbar snack = Snackbar.make(mainLayout, "Exercise deleted", Snackbar.LENGTH_SHORT);
                    snack.show();
                }
            });

Any idea how I could go about achieving this?

Thanks!

EDIT:

I made this incredibly crude drawing of the flow to make it clearer what I'm trying to achieve.

Plasma
  • 1,903
  • 1
  • 22
  • 37
  • Want you to show `Snack Bar` in previous *fragment* when you switch ? – Jay Rathod Jul 18 '16 at 09:47
  • @jaydroider Yes, the flow is something like this: Select item from list of items (the list is the "previous fragment") --> Get to detailed view of selected item ("current fragment"). Click to delete item --> Go back to list of items ("previous fragment") and show a snackbar. Did that make sense? – Plasma Jul 18 '16 at 09:50
  • basically what you are trying to do is to pass data between fragments that will indicate the deletion. you can use these answers to do this http://stackoverflow.com/questions/5194548/how-to-pass-data-between-fragments . The general idea is you use activity as interface between the two fragments. There are other methods like using roboguice and otto libraries. all those are detailed in the answers – john-salib Jul 18 '16 at 10:28

2 Answers2

0

I ended up implementing messaging between Fragments via the main Activity and checking for messages in the fragment's onResume() method:

MainActivity:

private String fragmentTransactionMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // Snip

        // Initialize fragment transaction message
        fragmentTransactionMessage = null;

        // Snip
    }

    // Three methods used to send information (text) between fragments
    public void setFragmentTransactionMessage(String message) {
        this.fragmentTransactionMessage = message;
    }

    public String getFragmentTransactionMessage() {
        return fragmentTransactionMessage;
    }

    public void resetFragmentTransactionMessage() {
        this.fragmentTransactionMessage = null;
    }

Fragment 2:

// Remove exercise from database
dbHandler.deleteExercise(exercise.getId());

// Update message in main activity
((MainActivity)getActivity()).setFragmentTransactionMessage("Item deleted");

// Move to previous fragment
getFragmentManager().popBackStack();

Fragment 1:

@Override
public void onResume() {
    super.onResume();

    // Check for messages in main activity
    String message = ((MainActivity)getActivity()).getFragmentTransactionMessage();

    // If any, display as snackbar
    if(message != null) {
        Snackbar snack = Snackbar.make(mainLayout, message, Snackbar.LENGTH_SHORT);
        snack.show();

        // Reset message in activity
        ((MainActivity)getActivity()).resetFragmentTransactionMessage();
    }
}
Plasma
  • 1,903
  • 1
  • 22
  • 37
0

Another suggestion is to wrap getFragmentManager().popBackStack(); in a method which takes a Runnable Obj which will run after a chosen delay

For Example:

public void goBack(Runnable runAfterBack, long delay) {
    mFragmentActivity.onBackPressed();
    if (runAfterBack != null) { 
    (new Handler(Looper.getMainLooper())).postDelayed(runAfterBack, delay);
     }
   }

Usage:

goBack(new Runnable() {
    @Override
    public void run() {
        Toast.makeText(mActivity, "STRING", Toast.LENGTH_SHORT).show();}
        }, 1500);
Tomer Petel
  • 334
  • 3
  • 11