Search code examples
androidandroid-fragmentsandroid-sliding

Opening fragment from another activity with additional information


I have an activity (activity A) that has a few sliding fragments. From one of the fragments (fragmentA, part of activity A) I am opening another activity (activity B) when a button is clicked on that fragment. Another fragment (fragment B, part of activity A) in the sliding menu has an option to add or delete something from the database whose buttons are in the action bar. Now from activity B, I am trying to finalize activity B and open the fragment B and then automatically have the "add something to database" item clicked. Basically I am trying to open a fragment from another activity. I'm not sure how I can do this.


Solution

  • In Activity B where you want to move to FragmentB Of ActivityA write

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction ft = fragmentManager.beginTransaction();
    ft.replace(R.id.content_frame, new FragmentB;  );
    ft.commit();  
    

    Where R.id.content_frame is layout id For your Activity A and for doing database with button in action bar write code in Activity A and FragmentB in onCreateOptionsMenu and in FragmentB write

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
             super.onActivityCreated(savedInstanceState);
                    setHasOptionsMenu(true);
    
            }
    

    Still You are not getting the solution than please post your code ..