更换TabActivity与FragmentActivity和片段 [英] Replacement of TabActivity with FragmentActivity and Fragments

查看:187
本文介绍了更换TabActivity与FragmentActivity和片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于TabActivity是pcated我需要找到一个方法来与片段做去$ P $。我曾与片段工作之前,我知道它是如何工作的,但我需要一个向导来创建我的标签主机FragmentActivities。我已经发现一对夫妇在互联网上的例子,他们都对把碎片放入容器的标签。

我要的是把FragmentActivities为每个标签。因为在我的应用程序将使用4个标签,每个标签都有它真的复杂的内容。其结果是,我需要有一个FragmentActivity为每个标签来管理,我将每个标签下把在一个分离容器中的片段。

解决方法:

在答案和搜索互联网上我结束了一个解决方案。据我已经找到了解决办法,我跟踪片段中分离堆栈的标签下主机的每个选项卡。 这里是我的GitHub库中,我创建了一个小型示例应用程序这个问题。

解决方案
  

由于TabActivity是德precated我需要找到一个方法来与片段做到这一点。

如果你不想使用 TabActivity - 忘记把 FragmentActivities 进入选项卡的内容。

我提醒,你可以使用 TabWidget 没有 TabActivity 。所以,你可以试试这个解决方案:

  1. 创建只是一个 FragmentActivity
  2. TabWidget FragmentActivity 的布局。让 TabWidget 的内容的身高= 0。
  3. TabWidget 在XML声明容器您片段 S(<$ C C $>的FrameLayout 为例)。
  4. FragmentActivity 只处理被选中的选项卡( TabHost.OnTabChangeListener ),并把需要的片段倒入容器中。
  5. 把PROGRAMM逻辑(早些时候在不同的活动),为不同的片段。

或者你也可以创建 FragmentActivity TabWidget ,而是切换片段■您可以直接把片段 s转换每个选项卡的内容。

例如,如果你有3个选项卡和3个片段尝试我做什么。呼叫 showFragmentX 当你需要改变一个片段到另一个。

 公共类测试扩展FragmentActivity {

私人片段1片段1 =新片段1();
私人Fragment2 fragment2 =新Fragment2();
私人Fragment3 fragment3 =新Fragment3();

私人无效showFragment1(){
    FragmentTransaction英尺= getSupportFragmentManager()的BeginTransaction()。
    ft.replace(R.id.fragments_container,片段1);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commit();
}

私人无效showFragment2(){
    FragmentTransaction英尺= getSupportFragmentManager()的BeginTransaction()。
    ft.replace(R.id.fragments_container,fragment2);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commit();
}

私人无效showFragment3(){
    FragmentTransaction英尺= getSupportFragmentManager()的BeginTransaction()。
    ft.replace(R.id.fragments_container,fragment3);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commit();
}

@覆盖
保护无效的onCreate(捆绑为arg0){
    // TODO自动生成方法存根
    super.onCreate(为arg0);
    的setContentView(R.layout.fragmentactivity_layout);
}
}
 

如果你这样做你的 fragmentX 变量不会每次你把它们放在 fragment_container 时间删除。他们将生活,而你的 FragmentActivity 生活。就拿看片段生命周期。 只有 OnCreateView onDestroyView 片段的方法,将调用一遍又一遍,而你替换一个片段到另一个。

此外片段都有自己的的onSaveInstanceState 方法,在这里您可以保存您的片段的状态。例如:用户在片段1的EDITTEXT输入他的名字。如果您想保留这个数据(名称字符串),而用户发现其他的片段,你应该

1.save的片段1的的onSaveInstanceState 方法
名字符串 2.到片段1的 onCreateView 方法检查savedInstanceState包,如果它不是空 - 填写的EditText用绳子,你从包获得

 公共类片段1扩展片段{

的EditText nameEditText;

@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
    super.onCreateView(充气,容器,savedInstanceState);

    查看fragment1View = inflater.inflate(R.layout.fragment1_layout,集装箱,假);
    nameEditText =(EditText上)fragment1View.findViewById(R.id.edittext_name);

    //检查保存的数据。如果不是空 - 填写您的EditText与保存的字符串
    如果(savedInstanceState!= NULL){
        字符串nameString =(字符串)savedInstanceState.get(nameString);
        nameEditText.setText(nameString);
    }
    返回fragment1View;
}

@覆盖
公共无效的onSaveInstanceState(包outState){
    super.onSaveInstanceState(outState);
    //保存数据的用户输入
    outState.putString(nameString,nameEditText.getText()的toString());
}
 

}

您可以在保存前检查数据。我希望现在我的观点是明确的。

另外,如果你调用 setRetainInstance(真) onCreateView()的方法,你的片段 - 系统将试图挽救片段的状态(与所有输入数据)。 href="http://stackoverflow.com/questions/11160412/why-use-fragmentsetretaininstanceboolean">链接

Since TabActivity is deprecated I need to find a way to do it with Fragments. I have worked with Fragments before I know how it works but I need a guide to create my tab host with FragmentActivities. I have found a couple of examples on the internet, and they are all about putting fragments into a container for the tabs.

What I want is to put FragmentActivities for each tab. Because in my application I will be using 4 tabs and each of the tabs have really complex content in it. As a result, I need to have a FragmentActivity for each tab to manage the Fragments that I will put under each tab within a separated container.

SOLUTION:

After answers and searching on internet I ended up with a Solution. According to the solution I have found, I keep track of Fragments in separated stack for each tab under tab host. Here is my GitHub repository, I created a small sample app for this problem.

解决方案

Since TabActivity is deprecated I need to find a way to do it with Fragments.

If you don't want to use TabActivity - forget about putting FragmentActivities into tab's content.

I remind that you can use TabWidget without TabActivity. So you can try this solution:

  1. Create just one FragmentActivity.
  2. Put TabWidget into FragmentActivity's layout. Make TabWidget's content's height = 0.
  3. Under TabWidget in XML declare container for you Fragments (FrameLayout for example).
  4. In FragmentActivity just handle which tab is selected (TabHost.OnTabChangeListener) and put needed Fragment into container.
  5. Put programm logics (which was earlier in different activities) into different Fragments.

Or you can create FragmentActivity with TabWidget, and instead of switching Fragments you can directly put Fragments into each tab's content.

For example if you have 3 tabs and 3 fragments try what i do. Call showFragmentX when you need to change one fragment to another.

public class Test extends FragmentActivity {

private Fragment1 fragment1=new Fragment1();
private Fragment2 fragment2=new Fragment2();
private Fragment3 fragment3=new Fragment3();

private void showFragment1(){
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.fragments_container, fragment1);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commit();
}

private void showFragment2(){
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.fragments_container, fragment2);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commit();
}

private void showFragment3(){
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.fragments_container, fragment3);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commit();
}

@Override
protected void onCreate(Bundle arg0) {
    // TODO Auto-generated method stub
    super.onCreate(arg0);
    setContentView(R.layout.fragmentactivity_layout);
}
}

If you do so your fragmentX variables will not be deleted each time you put them in fragment_container. They will live while your FragmentActivity live. Take look at fragments lifecycle. Only OnCreateView and onDestroyView methods of fragments will call again and again while you replace one fragment to another.

Also Fragments has their onSaveInstanceState method where you can save the state of your fragment. For example: user typed his name in fragment1's editText. If you want to keep this data(name string) while user discover other fragments you should

1.save name string in fragment1's onSaveInstanceState method
2. into fragment1's onCreateView method check savedInstanceState bundle, if it's not null - fill edittext with string that you get from bundle.

public class Fragment1 extends Fragment {

EditText nameEditText;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);

    View fragment1View=inflater.inflate(R.layout.fragment1_layout, container, false);
    nameEditText=(EditText) fragment1View.findViewById(R.id.edittext_name);

    //check for saved data. if it is not null - fill your edittext with saved string
    if(savedInstanceState!=null){
        String nameString=(String) savedInstanceState.get("nameString");
        nameEditText.setText(nameString);
    }
    return fragment1View;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    //save data that user have entered
    outState.putString("nameString", nameEditText.getText().toString());
}

}

You can check data before saving. I hope my point is clear now.

Also if you call setRetainInstance(true) in onCreateView() method of your fragment - system will try to save the state of fragment (with all input data). link to description

这篇关于更换TabActivity与FragmentActivity和片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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