简体   繁体   English

如何从活动的片段返回到viewpager中的片段

[英]How to return back to a fragment in viewpager from an Activity's fragment

I have a viewpager with 2 fragments(A and B) in MainActivity . 我在MainActivity有一个带有2个片段(A和B)的viewpager In the fragments in viewpager , I have a recyclerView for each fragment that contains a list of items. viewpager的片段中,每个包含项目列表的片段都有一个recyclerView When I click on an item of a fragment viewpager , it displays a new Activity with a new fragment (C) on it. 当我单击片段viewpager的项目时,它会显示一个新的Activity带有一个新的片段(C)。 But when I press the back button on toolbar, it always went back to the MainActivity with fragment A of viewpager , even if I click on item of fragment B. 但是,当我按下工具栏上的“后退”按钮时,即使单击片段B的项目,它也始终返回到viewpager片段A的MainActivity

In androidManifest.xml , I added "parentActivity: MainActivity" to the new Activity. androidManifest.xml ,我在新的Activity中添加了"parentActivity: MainActivity" If I don't set that, the back button would not response. 如果我没有设置,后退按钮将不会响应。 I need help to go back to the correct fragment when I press back button. 当我按下返回按钮时,我需要帮助以返回正确的片段。

I appreciate all answers. 我感谢所有答案。

Very easy. 很容易。 Keep an index variable in your activity in which viewpager and fragments are loaded. 在加载viewpager和片段的活动中保留一个索引变量。 When you switch to another activity, do it via this activities method and update that variable with the index of the current selected page/fragment in viewpager by 当您切换到另一个活动时,请通过此活动方法进行操作,并使用viewpager中当前所选页面/片段的索引更新该变量,方法是:

getCurrentItem getCurrentItem

This method will return the index which you can store then there are two options for you: If you know that your activity is going to be resumed from that activity only then you can write below method in onResume: 此方法将返回可以存储的索引,然后有两个选项供您选择:如果您知道您的活动将仅从该活动中恢复,则可以在onResume中编写以下方法:

setCurrentItem setCurrentItem

Or else you can go for startActivityForResult and achieve the same. 否则,您可以使用startActivityForResult并实现相同的目标。 Let me know if any queries. 让我知道是否有任何疑问。

First of all let assume you pressed something to open C fragment from B fragment. 首先,假设您按下了某些内容以从B片段中打开C片段。 Here you need to pass some value to inform that C was opened from B fragment. 在这里,您需要传递一些值以告知C是从B片段打开的。

Send data from fragment to activity 将数据从片段发送到活动

Intent intent = new Intent(getActivity().getBaseContext(),
                    TargetActivity.class);
            intent.putExtra("B", message);
            getActivity().startActivity(intent);

Here value "B" means that activity with fragment C is opened by fragment B. For A fragment you need to change it to "A" or whatever you want. 此处的值“ B”表示片段C会打开片段C的活动。对于片段,您需要将其更改为“ A”或任何您想要的内容。

Receive data in activity and send it back if Back button pressed 接收活动中的数据并按“返回”按钮将其发送回去

    Intent intent = getIntent();
    String message = intent.getStringExtra("message");
    back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SecondActivity.class, FirstActivity.class)
                intent.putString(message)
            }
        });

Receive data in FirstActivity and set necessary page 在FirstActivity中接收数据并设置必要的页面

Intent intent = getIntent();
String message = intent.getStringExtra("message");
if(message == "B"){
    viewPager.setCurrentItem(1) //1 is index of your second page 
}else {
    viewPager.setCurrentItem(0)
}

Try this code and let me know if something goes wrong 尝试这段代码,让我知道是否出现问题

First, for going to "NewActivity" just start the activity using intent and not finish the "MainActivity" . 首先,要转到“ NewActivity”,只需使用意图启动活动即可,而不要完成“ MainActivity”

And For coming back to "MainActivity", just finish the "NewActivity"... hopefully, this will work! 对于返回“ MainActivity”,只需完成“ NewActivity” ...希望可以!

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

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