문제

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.

도움이 되었습니까?

해결책

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 ..

다른 팁

One idea would be to have a global method for updating the DB. This way you can invoke it by clicking the ActionBarItems in your Activity A and by any time in your Activity B (or C,D...).

Another way would be to send a flag in your Intent you use to call A from B. Read the flag and if it fulfills your condition update your DB.

Also you could use startActivityForResult() and send a result from B to A which indicates that the DB should be updated.

As a hint: Don't burry functions which you have to call periodically in any App state. Make it more reachable.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top