如何拥有Android的动作栏后退按钮返回到片段 [英] How to have android action bar back button return to fragment

查看:117
本文介绍了如何拥有Android的动作栏后退按钮返回到片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好所有我已经在我的应用程序的操作栏来实现一个后退按钮,但我不知道如何从一个活动返回到片段或片段的片段。因此,如果有人能告诉我如何从一个活动恢复到一个片段甚至一个片段到另一片段将是惊人的。这里是code我现在

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);

}
        }

        }

和我的表现我有

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

推荐答案

最近,我不得不这样做,并决定switch语句是最好的一段路要走。从本质上讲,你跟踪你在现在的片段,然后切换到另一个根据您设置任何条件。对我来说,这是导航从一个片段到另一回。

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.

@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;
    }
}

我假设你的意思是你要结束你当前活动并显示一个片段?在这种情况下,很可能是父母FragmentActivity(主办的片段),所以你刚刚结束当前​​的活动,并在典型的方式开始另一个。再次:

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:

@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();

请记住,这仅仅是一个按钮,你基本上分配到情况下android.R.id.home onClick的()操作:

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

这篇关于如何拥有Android的动作栏后退按钮返回到片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆