Java 如何让android操作栏后退按钮返回片段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23330313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:58:09  来源:igfitidea点击:

How to have android action bar back button return to fragment

javaandroidandroid-fragmentsandroid-actionbarmanifest

提问by user3381665

Hello all I have implemented a back button in the action bar in my app but I dont know how to return from an activity to a fragment or a fragment to fragment. so if someone could show me how to return from an activity to a fragment or even a fragment to another fragment that would be amazing. Here is the code i have right now

大家好,我在我的应用程序的操作栏中实现了一个后退按钮,但我不知道如何从活动返回到片段或从片段返回到片段。所以如果有人能告诉我如何从一个活动返回到一个片段,甚至一个片段到另一个片段,那将是惊人的。这是我现在拥有的代码

public class Article extends Activity{
private WebView webview;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.articleview);
    // etc...
    getActionBar().setDisplayHomeAsUpEnabled(true);




    Bundle b = getIntent().getExtras();
    String KEY_LINK = b.getString("b");
    String url = getIntent().getStringExtra("key");

    WebView myWebView = (WebView) findViewById(R.id.webView);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    myWebView.loadUrl(url);

    myWebView.loadUrl(KEY_LINK);


    }


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        default:
            return super.onOptionsItemSelected(item);

}
        }

        }

and in my manifest I have

在我的清单中,我有

 <activity
        android:name=".Article"

        android:label="@string/app_name"
        android:screenOrientation="portrait">
        <intent-filter>

            <action android:name="com.NTCI.graffiti.Article" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.NTCI.graffiti.MainActivity" />
    </activity>

but i want it to return to its host fragment not the main activity I have it set to. Any thoughts?

但我希望它返回到它的主机片段而不是我设置的主要活动。有什么想法吗?

采纳答案by stewHymans

I recently had to do this and decided that a switch statement was the best way to go. Essentially you keep track of what fragment you're in now, and then switch to another one based on whatever criteria you set. For me, it was navigating back from one fragment to another.

我最近不得不这样做,并决定使用 switch 语句是最好的方法。从本质上讲,您可以跟踪您现在所处的片段,然后根据您设置的任何标准切换到另一个片段。对我来说,它是从一个片段导航回另一个片段。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
...
case android.R.id.home:
    switch(currentFragment){
    case FRAGMENT1:
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction
        .replace(R.id.fragment_container, fragment2);
        transaction.commit();
        currentFragment = FRAGMENT_2;
        return true;

    default:
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction
        .replace(R.id.fragment_container, fragment1);
        transaction.commit();
        currentFragment = FRAGMENT_1;
        return true;
    }
}

I'm assuming you mean you'd want to end the current activity you're in and display a fragment? In this case there'd probably be a parent FragmentActivity (to host the fragment), so you'd just end the current Activity and start another in the typical way. Once again:

我假设您的意思是您想要结束当前的活动并显示一个片段?在这种情况下,可能会有一个父 FragmentActivity(用于托管片段),因此您只需结束当前 Activity 并以典型方式启动另一个。再来一次:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
...
case android.R.id.home:

Intent intent = new Intent(this, NewFragmentActivity.class);
intent.putExtra(...); // so you can pass what activity you're coming from, if needed
startActivity(intent);
this.finish();

Keep in mind this is just a button and you're essentially assigning the onClick() action through case android.R.id.home:

请记住,这只是一个按钮,您实际上是通过以下方式分配 onClick() 操作 case android.R.id.home: