【问题标题】:Proper Up Navigation From Fragment to Activity从片段到活动的正确导航
【发布时间】:2013-08-31 15:58:41
【问题描述】:

我目前正在开发一个包含管理多个片段的基本活动的应用程序。其中一个片段是一个列表,当单击一个项目时会启动一个详细信息活动。我希望当我单击主页按钮 ( Using ABS ) 让应用程序导航回 listfragment 但它总是导航回我指定为默认值的片段。

Base Activity
    Frag1 - Default
    Frag2
    Frag3 -> DetailsActivity

当从 DetailsActivity 按下主页按钮时,我想返回 Frag3,而不是转到 Frag1。

任何帮助都会很棒。这是我在细节活动中处理按下主页按钮的代码。

主要活动:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // set the Above View
    if (savedInstanceState != null)
        mContent = getSupportFragmentManager().getFragment(savedInstanceState,               "mContent");
    if (mContent == null)
        mContent = new HomeFragment();

    // set the Above View
    setContentView(R.layout.content_frame);
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.content_frame, mContent)
            .commit();

    // set the Behind View
    setBehindContentView(R.layout.menu_frame);
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.menu_frame, new SlidingMenuFragment())
            .commit();

    // customize the SlidingMenu
    getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    getSupportFragmentManager().putFragment(outState, "mContent", mContent);
    super.onSaveInstanceState(outState);
}

public void switchContent(Fragment fragment) {
    getSlidingMenu().showContent();

    mContent = fragment;
    getSupportFragmentManager()
            .beginTransaction()
            .addToBackStack("test")
            .replace(R.id.content_frame, fragment)
            .commit();
}

子活动:

@Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
    switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

【问题讨论】:

    标签: android android-fragments actionbarsherlock android-navigation


    【解决方案1】:

    拦截事件--home--详细activity,调用'finish()'。

    在 frag#3 中实现 'on activity result()'。

    您将从详细活动返回到 frag3。

    【讨论】:

    • 我能够让它工作,但是,这似乎不是最佳实践。我不期望从细节活动中得到结果,所以我宁愿不使用这种方法,除非它确实是唯一的方法。感谢您的技巧,尽管它确实有效!
    • 那么看看使用 frag3 中的接口在事件 frag#3.HOME 上实际返回 BaseActivity,然后从 Base 启动 Intent for DetailActivity。当 Detail Activity 完成后,使用 frag transactionManager 在 Detail 之后“添加”frag#3。
    【解决方案2】:

    您需要将片段添加到后台堆栈。见Implement Back Navigation for Fragments

    【讨论】:

    • 我认为问题在于当我返回主活动时,savedInstanceState 始终为空。我在上面添加了更多代码。
    【解决方案3】:

    如果向您的父 Activity(Base Activity)传递一个额外的内容,例如您的 Fragment id?

    子活动:

    @Override
    public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
        switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button
            case android.R.id.home:
            // Retrieve this Activiy's parent Up intent
            Intent upIntent = NavUtils.getParentActivityIntent(this);
            // Add the required Intent extras as appropriate
            upIntent.putExtra("KEY", id);
            NavUtils.navigateUpTo(this, upIntent);
    
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    

    NavUtils.navigateUpFromSameTask(this) 相当于调用navigateUpTo(sourceActivity, getParentActivityIntent (sourceActivity))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      相关资源
      最近更新 更多