Training—Building a Dynamic UI with Fragments

阅读:https://developer.android.com/training/basics/fragments/index.html

 

关于低版本兼容Fragment的,V4支持包的Activity使用FragmentActivity,V7的使用 ActionBarActivity。

文字使用到了自适应,系统会根据屏幕的不同来选择不同的布局文件,所以当你要从代码进行不同的布局的时候,要根据布局文件的某些特征。

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_articles);

        // Check that the activity is using the layout version with
        // the fragment_container FrameLayout
        if (findViewById(R.id.fragment_container) != null) {

            // However, if we're being restored from a previous state,
            // then we don't need to do anything and should return or else
            // we could end up with overlapping fragments.
            if (savedInstanceState != null) {
                return;
            }

            // Create a new Fragment to be placed in the activity layout
            HeadlinesFragment firstFragment = new HeadlinesFragment();
            
            // In case this activity was started with special instructions from an
            // Intent, pass the Intent's extras to the fragment as arguments
            firstFragment.setArguments(getIntent().getExtras());
            
            // Add the fragment to the 'fragment_container' FrameLayout
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, firstFragment).commit();
        }
    }
}

根据以上代码的注释来看,ACtivity的savedInstanceState包含了Fragment的信息,因此如果savedInstanceState不为空,就没必要重新new新的Fragment了。

Keep in mind that when you perform fragment transactions, such as replace or remove one, it's often appropriate to allow the user to navigate backward and "undo" the change. To allow the user to navigate backward through the fragment transactions, you must call addToBackStack() before you commit the FragmentTransaction.

Note: When you remove or replace a fragment and add the transaction to the back stack, the fragment that is removed is stopped (not destroyed). If the user navigates back to restore the fragment, it restarts. If you do not add the transaction to the back stack, then the fragment is destroyed when removed or replaced.

  如果要让用户能够UNDO,记得在commit之前使用addToBackStack()。一旦使用了addToBackStack()而且使用了replace,之前的Fragment只是stop,但是如果没有使用addToBackStack(),那么Fragment直接被摧毁。

  The addToBackStack() method takes an optional string parameter that specifies a unique name for the transaction. The name isn't needed unless you plan to perform advanced fragment operations using the FragmentManager.BackStackEntry APIs.

  这段话似乎给某种高级用法提了个头,addToBackStack()的参数应该是个切口。

  关于Fragment的通信,http://www.cnblogs.com/yutoulck/p/3378463.html 里面有说明,不再累述,总而言之,Fragment之间不得直接联系。

  而系统准许Activity直接调用Fragment的公有方法。

 

 

转载于:https://www.cnblogs.com/yutoulck/p/3402942.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值