t通过选择项目previous片段调用后popBackStack

t通过选择项目previous片段调用后popBackStack

本文介绍了如何从ListFragment通过选择项目previous片段调用后popBackStack()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有prepared一个非常简单的测试情况与我的问题1活性2片段:

I have prepared a very simple test case with 1 activity and 2 fragments for my question:


  • (存储在共享preferences选择的项目)

  • (显示一个文本视图和一个选择按钮)

  • (显示项目列表)

  • MainActivity.java (stores selected item in shared preferences)
  • MyMainFragment.java (displays a text view and a "select" button)
  • MyListFragment.java (displays a list of items)

最初MainActivity显示MyMainFragment。

Initially MainActivity displays MyMainFragment.

当用户触摸选择项...按钮,MainActivity显示MyListFragment有:

When user touches "Select item..." button, MainActivity displays MyListFragment with:

public void selectedButtonClicked() {
    SharedPreferences prefs = getSharedPreferences(PREFS, 0);
    int index = prefs.getInt(INDEX, -1);
    Fragment fragment = MyListFragment.newInstance(index);
    getFragmentManager().beginTransaction()
        .addToBackStack(null)
        .replace(R.id.root, fragment, LIST)
        .commit();
}

用户触摸列表中的项目之一后,MainActivity存储在共享preferences和电话所选择的项目位置 popBackStack()来再次显示MyMainFragment:

After user touches one of the items in the list, MainActivity stores the selected item position in shared preferences and calls popBackStack() to display the MyMainFragment again:

public void itemSelected(int index) {
    SharedPreferences prefs = getSharedPreferences(PREFS, 0);
    Editor editor = prefs.edit();
    editor.putInt(INDEX, index);
    editor.commit();
    getFragmentManager().popBackStack();
    // How to show the index in mSelectedTextView?
}

即。在我的应用程序在MainActivity做所有的繁重的工作:它显示的2片段1,并保持在共享preferences用户数据

I.e. the MainActivity in my app does all the "heavy work": it displays 1 of 2 fragments and keeps the user data in shared preferences.

我的问题是:如何在 mSelectedTextView 的MyMainFragment的显示所选择的项目(在上面的截图项目07)

My question is: how to display the selected item (the "Item 07" in the above screenshot) in the mSelectedTextView of the MyMainFragment?

我正在寻找一种方式来做到这一点正确,没有任何黑客(例如碎片不应该接触的共享preferences)。

I'm looking for a way to do it properly, without any hacks (for example fragments shouldn't touch shared preferences).

更新:我试图corsair992的建议(!感谢)

UPDATE: I've tried the suggestion by corsair992 (thanks!)

public void itemSelected(int index) {
    SharedPreferences prefs = getSharedPreferences(PREFS, 0);
    Editor editor = prefs.edit();
    editor.putInt(INDEX, index);
    editor.commit();
    getFragmentManager().popBackStackImmediate();
    MyMainFragment f = (MyMainFragment) getFragmentManager().findFragmentById(R.id.root);
    // call a method in MyMainFragment to update mSelectedTextView
    f.setText("Selected index: " + index);
}

但它仅适用,当用户在触摸MyListFragment一个项目。但是这不工作,当用户返回触摸返回按钮MyMainFragment。

but it only works, when user touches an item in MyListFragment. It does not however work, when user returns to MyMainFragment by touching Back button.

推荐答案

在我看来,你已经有了一个合理的第一部分答案(由corsair992建议 - 从选择的项目传递值),第二部分是如何你怎样对待返回键。

As I see it, you already have a reasonable first part to the answer (suggestion by corsair992 - pass the value from the item selected) the second part is how do you handle the back key.

有这样做(听了后栈等),但我用最fequently之一的几个方法是:

There are a few ways of doing that (listening to the back stack etc.) but the one I have used most fequently is:

声明一个返回键监听器接口:

declare a back key listener interface:

public interface BackKeyListener {
   boolean onBackPressed();
}

在您的主要活动添加以下内容:

in your main activity add the following:

import BackKeyListener;
void onBackKeyPressed() {
   boolean backKeyHandled = false;
   Fragment activeFragment = getActiveFragment();
   if (activeFragment instanceof BackKeyListener) {
      backKeyHandled = ((BackKeyListener) activeFragment).onBackKeyPressed();
   }
   if (!handled) {
      // If should process back key normally, pass to super..
      super.onBackKeyPressed();
   }
}

Fragment getActiveFragment() {
   // I'm assuming you place all your fragments here, if not..
   // alter to suit
   return getFragmentManager().findFragmentById(R.id.root);
}

在MyListFragment添加以下内容:

in MyListFragment add the following:

import BackKeyListener;

class MyListFragment extends Fragment(orsomething) implements BackKeyListener

public boolean onBackPressed() {
  // User pressed back, select default item..
  ((MainActivity) getActivity()).itemSelected(selectedItem);

  // In this case, prevent normal back processing or
  // you might get a double backstack pop.
  return true;
}

在片段使用的界面让你不小心的活性片段是什么。
如果它实现了BackKeyListener,在MainActivity将调用它时,返回键是pssed $ P $。
这给你传递一个值回活动的选项。

Using an interface on the fragment allows you to "not care" what the active fragment is.If it implements the BackKeyListener, the MainActivity will call it when the back key is pressed.This gives you the option of passing a value back to the activity.

这篇关于如何从ListFragment通过选择项目previous片段调用后popBackStack()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 19:06