R.Youssef
R.Youssef

Reputation: 169

How can i make a toast when user swipes to first fragment

I have three fragments. When the user is on the third fragment and he swipes to the second fragment I want a toast to show up. Is that possible ?

Upvotes: 2

Views: 65

Answers (1)

Susmit Agrawal
Susmit Agrawal

Reputation: 3764

If you want to show a toast anytime the fragment becomes visible, then use onResume in the fragment:

@Override
public void onResume() {
    super.onResume();
    //Make your toast here
}

But in case you are particular about the previous fragment, use a static variable in the Activity containing the fragments.

For example, if the activity is named MainAvtivity:

class MainAvtivity extends Activity {
    static int currentFrag = -1;
    ....
}

Then, in the onResume method of the fragment, do something like:

@Override
public void onResume() {
    super.onResume();
    if(MainActivity.currentFrag == 3)
        //Make toast here
    MainActivity.currentFrag = <current_fragment_number>;
}

Upvotes: 1

Related Questions