We had to implement exactly that same behaviour that you describe for an app recently. The screens and overall flow of the application were already defined so we had to stick with it (it s an iOS app clone...). Luckily, we managed to get rid of the on-screen back buttons :)
We hacked the solution using a mixture of TabActivity, FragmentActivities (we were using the support library for fragments) and Fragments. In retrospective, I m pretty sure it wasn t the best architecture decision, but we managed to get the thing working. If I had to do it again, I d probably try to do a more activity-based solution (no fragments), or try and have only one Activity for the tabs and let all the rest be views (which I find are much more reusable than activities overall).
因此,要求在每个表格中设置一些表格和板块:
tab 1
screen 1 -> screen 2 -> screen 3
tab 2
screen 4
tab 3
screen 5 -> 6
......
因此,他说,用户开始在表1中,从屏幕1到屏幕2,然后到屏幕3,然后从屏幕4到6转至表3和导航器;如果转至表1,他应当再次看到屏幕3,如果他敦促他回击,他应当返回屏幕。 2; 重整,他接受1号屏幕;改用表3,再用6号屏幕。
The main Activity in the application is MainTabActivity, which extends TabActivity. Each tab is associated with an activity, lets say ActivityInTab1, 2 and 3. And then each screen will be a fragment:
MainTabActivity
ActivityInTab1
Fragment1 -> Fragment2 -> Fragment3
ActivityInTab2
Fragment4
ActivityInTab3
Fragment5 -> Fragment6
每项活动 在Tab, 当时只有一片碎片,知道如何将一块碎片替换成另一块碎片(通常与行动小组相同)。 冷却的意思是,为每一条铺设的背后cks,很容易分离。
The functionality for each ActivityInTab was quite the same: know how to navigate from one fragment to another and maintain a back stack, so we put that in a base class. Let s call it simply ActivityInTab:
abstract class ActivityInTab extends FragmentActivity { // FragmentActivity is just Activity for the support library.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_in_tab);
}
/**
* Navigates to a new fragment, which is added in the fragment container
* view.
*
* @param newFragment
*/
protected void navigateTo(Fragment newFragment) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.content, newFragment);
// Add this transaction to the back stack, so when the user presses back,
// it rollbacks.
ft.addToBackStack(null);
ft.commit();
}
}
这项活动就是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:isScrollContainer="true">
</RelativeLayout>
你可以看到,每个表格的表述也是一样的。 这是因为,它只是一个零碎的缩影,它称作每个碎块的内容。 碎块是每一条屏幕的碎片。
就奖金点而言,我们还增加了一些小码,以显示用户 press击时的确认方言,而且不再有碎片可追溯到:
// In ActivityInTab.java...
@Override
public void onBackPressed() {
FragmentManager manager = getSupportFragmentManager();
if (manager.getBackStackEntryCount() > 0) {
// If there are back-stack entries, leave the FragmentActivity
// implementation take care of them.
super.onBackPressed();
} else {
// Otherwise, ask user if he wants to leave :)
showExitDialog();
}
}
That s pretty much the setup. As you can see, each FragmentActivity (or just simply Activity in Android >3) is taking care of all the back-stacking with it s own FragmentManager.
An activity like ActivityInTab1 will be really simple, it ll just show it s first fragment (i.e. screen):
public class ActivityInTab1 extends ActivityInTab {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
navigateTo(new Fragment1());
}
}
然后,如果碎片需要穿透另一个碎片,它就不得不做些什么新生......但不是这样坏:
// In Fragment1.java for example...
// Need to navigate to Fragment2.
((ActivityIntab) getActivity()).navigateTo(new Fragment2());
因此,情况恰恰相反。 我确信,这不是一个非常坦率的(而且大多肯定不好)解决办法,因此,我要问季安乐施会如何更好地发挥这一功能,如果这不是安乐的“怎么做”,我不希望你能够向我指出一些链接或材料,解释什么是<><>>>>><><>>>>>? 处理这一问题的方法(表格、表格中的nes屏等)。 Feel Feel comments
As a sign that this solution is not very good is that recently I had to add some navigation functionality to the application. Some bizarre button that should take the user from one tab into another and into a nested screen. Doing that programmatically was a pain in the butt, because of who-knows-who problems and dealing with when are fragments and activities actually instantiated and initialized. I think it would have been much easier if those screens and tabs were all just Views really.
Finally, if you need to survive orientation changes, it s important that your fragments are created using setArguments/getArguments. If you set instance variables in your fragments constructors you ll be screwed. But fortunately that s really easy to fix: just save everything in setArguments in the constructor and then retrieve those things with getArguments in onCreate to use them.