Seshu
Seshu

Reputation: 137

How to provide back button in fragments programmatically?

I have one Activity with two fragments.here i provide one back button in 2nd fragment programatically to goback to 1st fragment.

here my fragment1 code:

 public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
    Bundle savedInstanceState) {
    // Inflate the layout for this fragment
      V = inflater.inflate(R.layout.lm_fragement, container, false);
     listview = (ListView)V.findViewById(R.id.listView1);
     url="http://victorysites.org/apps/SSPC/homeinfo.php?UDID=12341234";

     JsonParsing json=new JsonParsing();
     list=json.getData(url);
     System.out.println("length :"+list.size());

     listview.setAdapter(new MyAdapter(getActivity(),list));


     final ProgressDialog waitdialog;



      listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // TODO Auto-generated method stub
            Bundle data = new Bundle();
            data.putString("name",""+arg2);
            Fragment f;
            f = new PM_Fragment();
            f.setArguments(data);
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.replace(android.R.id.content, f);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            ft.addToBackStack(null);
            ft.commit(); 
        }
    });

    Button bt=(Button)V.findViewById(R.id.button1);
    bt.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Bundle data = new Bundle();
            data.putString("name","Venkat");
            Fragment f;
            f = new PM_Fragment();
            f.setArguments(data);
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.replace(android.R.id.content, f);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            ft.addToBackStack(null);
            ft.commit();
        }
    });


    return V;
}

}

Here my fragment2 code:

  public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
    Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    f=new PM_Fragment();
      V = inflater.inflate(R.layout.pm_fragment, container, false);
     Bundle bundle = this.getArguments();
      String myInt = bundle.getString("name");
      System.out.println("NAME"+myInt);
      TextView tv=(TextView)V.findViewById(R.id.portrait_message);
      tv.setText(myInt);

    Button bt=(Button)V.findViewById(R.id.button1);
    bt.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            /*Fragment f=new PM_Fragment();
            getActivity().getSupportFragmentManager().beginTransaction().remove(f).commit();*/
            //getActivity().getFragmentManager().beginTransaction().remove(this).commit();
        getFragmentManager().popBackStack();


        }
    });


    return V;
}

}

its working fine,but the problem is it reloads the previous(1st fragment) screen.But i don't want reload the page,i want its working like default back button in emulator.

Anyone can give me an idea. Thanks in advance.

Upvotes: 4

Views: 4686

Answers (1)

Augustus Francis
Augustus Francis

Reputation: 2670

When using fragments in your app, individual FragmentTransaction objects may represent context changes that should be added to the back stack. If you are implementing a master/detail flow on a handset by swapping out fragments, you should ensure that pressing the Back button on a detail screen returns the user to the master screen. To do so, call addToBackStack() before you commit the transaction:

// Works with either the framework FragmentManager or the
// support package FragmentManager (getSupportFragmentManager).
getSupportFragmentManager().beginTransaction()
                           .add(detailFragment, "detail")
                           // Add this transaction to the back stack
                           .addToBackStack()
                           .commit();

*When there are FragmentTransaction objects on the back stack and the user presses the Back button, the FragmentManager pops the most recent transaction off the back stack and performs the reverse action (such as removing a fragment if the transaction added it).*

Note: You should not add transactions to the back stack when the transaction is for horizontal navigation (such as when switching tabs) or when modifying the content appearance (such as when adjusting filters). For more information, about when Back navigation is appropriate, see the Navigation design guide.

ADDED

If your application updates other user interface elements to reflect the current state of your fragments, such as the action bar, remember to update the UI when you commit the transaction. You should update your user interface after the back stack changes in addition to when you commit the transaction. You can listen for when a FragmentTransaction is reverted by setting up an FragmentManager.OnBackStackChangedListener:

getSupportFragmentManager().addOnBackStackChangedListener(
        new FragmentManager.OnBackStackChangedListener() {
            public void onBackStackChanged() {
                // Update your UI here.
            }
        });

Please refer this link for more details

Upvotes: 1

Related Questions