简体   繁体   English

Android-活动片段并返回

[英]Android - fragment to activity and back

In Android, when we replace a container view with a new fragment, we can use replace() and addToBackStack( ), so on pressing back button we goto previous fragment. 在Android中,当我们用新的片段替换容器视图时,可以使用replace()和addToBackStack(),因此在按返回按钮时,我们转到了先前的片段。

But what if the following occurs : 但是,如果发生以下情况,该怎么办:

Activity1 (fragment1 -> fragment2 [calls startActivity for Activity2]) -> Activity2 活动1(片段1->片段2 [为活动2调用startActivity])->活动2

Within Activity1, I can press back button to go from fragment2 to fragment1. 在Activity1中,我可以按“后退”按钮从fragment2转到fragment1。 But when fragment2 starts another activity, on pressing back button from Activity2, it takes me to fragment1 in Activity1. 但是,当fragment2启动另一个活动时,在从Activity2按下返回按钮时,它将带我到Activity1中的fragment1。 How can I make back button press from Activity2 come back to fragment2 in Activity1 ? 我如何才能使从Activity2按下后退按钮返回到Activity1中的fragment2?

Code : 代码:

// In Activity1 - starts with a ListFragment
@Override
protected void onResume() {
    super.onResume();

    getFragmentManager().beginTransaction()
            .replace(R.id.container, ListFragment.newInstance(0))
            .commit();
}

// In Activity1 , each item in list replaces the container view 
// with new fragment
@Override
public void onItemSelected(int position) {

    if(position == 0) {

        getFragmentManager().beginTransaction()
                .replace(R.id.container, Example1_Fragment.newInstance(0))
                .addToBackStack(null)
                .commit();

    }
   ....
 }

 // In Example1_Fragment

public class Example1_Fragment extends Fragment {

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.example1_fragment, container, false);

    Button btnIntent = (Button) view.findViewById(R.id.btnIntent);
    btnIntent.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent();
            intent.setAction("com.android.test2.app.example1_action");
            startActivityForResult(intent,1);
        }
    });

    return view;
}

// this is why I want to come back to Example1_Fragment - 
// the activity I start has to send a result back to
// the Example1_Fragment, but on back button, it takes me
// back to the ListFragment, and I cannot take any UI
// action (change to UI) in Example1_Fragment, as the
// ListFragment gets displayed - the onActivityResult of
// Example1_Fragment does get called

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == 1 && resultCode == Activity.RESULT_OK){

             // do something
    }
}

不要在onResume()回调中替换Fragment1,返回到activity1时将始终调用此方法,请在onCreate()回调中进行此操作。

You can manually set the back pressed calls inside of this method in your Second Activity. 您可以在“第二个活动”中的此方法中手动设置后按调用。 So that when you press back, you can find fragment 2 by its tag and add it back. 这样,当您按回时,可以通过其标签找到片段2并将其添加回去。

@Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
            Intent i = new Intent(this, firstActivity.class);
            startActivity(i);
            FragmentTwo fragmentTwo = manager.findFragmentByTag("fragmentTwoTag");
            manager.beginTransaction.replace(fragmentOne, fragmentTwo).addToBackStack(null).commit();


    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM