user3800832
user3800832

Reputation: 421

how to back to previous fragment on pressing manually back button

I have one activity and multiple fragments in my app. i want to back one by one fragments when pressing back button which in all fragments.

i used this code segment but when pressing back button it comes to main activity without back one by one. Also i want to change the icon when it's comes to the main activity.(msg_alert)

 btnBack.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentManager fm = MainActivity.this
                    .getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            Fragment fragment = new MainMenuLayout();
            ft.replace(R.id.activity_main_content_fragment, fragment);
            ft.commit();
            btnBack.setVisibility(View.VISIBLE);
            btnBack.setImageResource(R.drawable.msg_alert);
            tvTitle.setText("Layout 0");

        }
    });

Upvotes: 1

Views: 2678

Answers (5)

vijay_t
vijay_t

Reputation: 786

Lets say you are having two fragments A and B. Fragment A is is attached at the startup of activity and on any user event you navigate to fragment B by replacing the Fragment A.

1) while adding Fragment B to the activity

// Works with either the framework FragmentManager or the
// support package FragmentManager (getSupportFragmentManager).
getSupportFragmentManager().beginTransaction()
                           .replace(R.id.container, fragmentB, "detail")
                           // Add this transaction to the back stack
                           .addToBackStack()
                           .commit();

2) override the onBackPressed of the activity to handle the back button press event.

@override
public void onBackPressed() {
     // is there any fragment in backstack, if yes popout.
    if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
        getSupportFragmentManager().popBackStack();
        return;
    }
    super.onBackPressed();
}

Upvotes: 2

Pravin
Pravin

Reputation: 1362

I seen in your code that you are only replace the content frame. and so the result is backpress it comes to activity.
Also be sure that you have your mannual back button in the activity
this is test xml for activity

<?xml version="1.0" encoding="utf-8"?>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Back" />

<FrameLayout
    android:id="@+id/activity_main_content_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</FrameLayout>

Use

Fragment fragment = new MainMenuLayout();
                    ft.replace(R.id.activity_main_content_fragment, fragment);
                    ft.addToBackStack(null);// TODO parameter here it tag of fragment or null or "" String
                    ft.commit();


For more information about fragments backstack go through this android developer refrence :-Fragment BackStack

Upvotes: 0

user2047685
user2047685

Reputation: 1

In Fragments we dont have back navigation..

We can acheive that through replace() method.

Upvotes: 0

Pragnesh Ghoda  シ
Pragnesh Ghoda シ

Reputation: 8337

You have to Use addToBackStack() method while doing Transaction Between Fragments...

Read This : Implement Back Navigation for Fragments

Use Following code when you use Fragment Transaction...

String backStateName = fragment.getClass().getName(); // getting Fragment Name..
FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment)
    .addToBackStack(backStateName) //adding it to BackStack..
    .commit();

Upvotes: 0

Rajesh Mikkilineni
Rajesh Mikkilineni

Reputation: 844

this is one more option ,

in the Activity.

Fragment secondfragment= new SecondFragmnet();

 @Override
public void onBackPressed() {

     if(secondfragment.isVisible()){
    // replace 1st fragment
      }else{
         // Alert dialog for Exit App
     }
// you can check multiple fragments.

Hope it helps you

Upvotes: 0

Related Questions