简体   繁体   English

如何使Android操作栏后退按钮返回片段

[英]How to have android action bar back button return to fragment

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? 有什么想法吗?

I recently had to do this and decided that a switch statement was the best way to go. 我最近不得不这样做,并决定使用switch语句是最好的方法。 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;
    }
}

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. 在这种情况下,可能会有一个父FragmentActivity(用于托管片段),因此您只需结束当前的Activity并以典型的方式启动另一个即可。 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();

Keep in mind this is just a button and you're essentially assigning the onClick() action through case android.R.id.home: 请记住,这只是一个按钮,您实际上是通过case android.R.id.home:分配onClick()操作case android.R.id.home:

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

相关问题 如何覆盖android中的操作栏后退按钮? - how to override action bar back button in android? 如何在已打开活动的操作栏中向片段添加后退按钮? - How to add a back button to the fragment in the action bar of an opened activity? 动作栏上的“返回”按钮-Android。 如何“回头”? - “Back” button on Action bar - Android . How to go “back”? Android如何在活动中添加返回按钮以在片段活动中返回 - Android how to add back button in activity to return back in fragment activity 在Android中完成动作后如何返回相同的片段类 - How return back to same fragment class after complete action in android 如何在导航栏中创建后退按钮的片段? - how to create back button in navigation bar in a fragment? Android:如何在后退按钮操作栏和设备的后退按钮上保留对象的状态 - Android: How can I retain the state of an object on back button Action bar and back button of device 如何让活动返回按钮返回片段? - How to get activity back button return to fragment? 打开片段时如何将操作栏图标更改为后退箭头 - how to change Action bar icon to back arrow when open a fragment 如何在不启用操作栏上的后退按钮图标的情况下启用手机的后退按钮到 go 回到上一个活动? - How to enable back button of phone to go back to previous activity without enabling back button icon on action bar?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM