Android Activity和Fragment之间的跳转方式

阅读数:343 评论数:0

跳转到新版页面

分类

python/Java

正文

一、预备知识

Fragment必须嵌入到Activity中, 当Activity暂停时, 其中的所有片段也会暂停, 当Activity被销毁时,所有片段也会被销毁. 不过,  当Activity正在运行时,可以独立操纵每个Fragment, 如添加或移除它们. 当执行此类Fragment事务时, 也可以将其添加到由Acitivy管理的返回栈中.

通过扩展Fragment类或其子类来创建一个片段, 没有用户界面的片段, 充当该片段所嵌入到Activity的一个worker, 当作Activity的不可见工作线程.

要在一个Activity中使用片段, 可以选择在布局文件中使用Fragment元素来声明. 在android:name属性中指定要在布局中实例化的Fragment类, 并且用android:id属性指定一个标识符, 每个片段都需要一个唯一的标识符, 可以通过三种方式为Fragment提供id:

(1) 为android:id属性提供唯一id

(2)为android:tag属性提供唯一字符串

(3)如果未给以上两个属性提供值, 系统会使用容器视图的id

<fragment
        android:id="@+id/fragment_select"
        android:name="com.example.zy.myapplication.SelectFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

当系统创建Activity布局时, 会实例化在布局中指定的每个片段, 并为每个片段调用onCreateView()方法以检索每个片段的布局. 系统会直接插入片段返回的View来替代<Fragment>元素 .

此外, 也可以通过代码在Activity运行期间随时将Fragment添加到Activity布局中,  在Activity中通过使用FragmentManager来管理片段, 通过调用getFragmentManager()或者getSupportFragmentManager()来获取FragmentManager的实例, 然后在FragmentManager调用beginTransaction()方法来获取FragmentTransaction对象, 从而得以执行Fragment事务.

二、准备工作

1. 布局

在MainActivity的布局文件中写一个子布局

  <FrameLayout    
        android:id="@+id/fragment_container"    
        android:layout_width="match_parent"    
        android:layout_height="0dp"   
        android:layout_weight="1"/>

2. 创建两个java类文件继承v4包的Fragment, 并重写onCreateView方法引用对应的Fragment布局文件 (下面以其中一个为例)

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    if (contentView == null) {  
        contentView = inflater.inflate(R.layout.my_fragment, container, false);   
        Button myButton = (Button) contentView.findViewById(R.id.my_button);        
        Button myReturn = (Button) contentView.findViewById(R.id.my_return);     
        Button myOther = (Button) contentView.findViewById(R.id.my_other);    
        myButton.setOnClickListener(this);  
        myReturn.setOnClickListener(this);     
        myOther.setOnClickListener(this); 
 }  
    return contentView;
}

3. 在MainActivity中先添加进一个Fragment进行最开始的展示(压栈式添加)

getSupportFragmentManager()
.beginTransaction()
      .replace(R.id.fragment_container,new MyFragment())
      .addToBackStack(null)
      .commit();

4. 在Fragment类中对几个按钮进行点击监听, 在点击方法对几个跳转进行操作(代码省略)

三、跳转方式

1、从同一个Activity的Fragment跳转到另一个Fragment

getActivity().getSupportFragmentManager() 
    .beginTransaction()
    .replace(R.id.xx, new XxxFragment(), null) 
    .addToBackStack(null)
    .commit();

说明:

R.id.xx: Fragment对应的Activity布局中FragmentLayout的id

new XxxFragment(): 要跳转到的Fragment

addToBackStack(null): 可以省略不写(不写表示为非压栈式添加)

2、从一个Activity的Fragment跳转到另外一个Activity

Intent intent = new Intent(getActivity(),OtherActivity.class); 
startActivity(intent);

3、从一个Activity跳转到另外一个Activity的Fragment

Intent intent = new Intent(OtherActivity.this, MainActivity.class);
intent.putExtra("id",1);
startActivity(intent);

然后,我们在MainActivity里接收id值, 对值进行判断, 如果正确进行跳转操作:

 int id = getIntent().getIntExtra("id", 0);
 if (id == 1) {      
      getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.fragment_container,new YourFragment())
        .addToBackStack(null)
        .commit(); 
 }

4、从一个Activity的Fragment跳转到另外一个Activity的Fragment上

这种跳转与第三种跳转极为类似, 我们只需要将上面的

Intent intent = new Intent(getActivity(), MainActivity.class);

写在对应的Fragment中, 其他不用改变.




相关推荐

程序启动图标(Logo)\底部菜单图标 小屏ldpi() 36x36 px 中屏mdpi(160dpi) 48x48 px 大屏hdpi(240dp

该属性是当一个view获取焦点时, 定义ViewGroup和其子控件两者之间的关系, 属性的值有三种: (1) beforeDescendants: viewgroup会优先其子控

只有在LinearLayout时, 该属性才有效. android:layout_w

gravity的中文意思就是"重心", 就是表示view横向和纵向的依靠位置. a

android webview从Lollipop(5.0)开始webview默认不允许混合模式, https当中不能加载http资源, 而开发的时候可能使用的是https的链接, 但是链接中的图

从网上查找资料,发现有多种方式, 我用了其中最简单的方式: 使用java.lang.String的replace方法, <pre class="language-marku

在Anrdoid自定义View时候,需要使用TypedArray来获取XML layout中的属性值,使用完之后,需要调用recycle()方法将TypeArray回收。 那么Ty

当我们的项目的某些属性和第三方库中属性有冲突或者我们想修改第三方库中某些资源时,我们就需要使用tools:replace来处理。 1、有冲突的情况 比如第三方库中

在Android中,使用Span定义文本的样式,通过Span改变几个文字的颜色,Span能够改变TextPaint属性,在Canvas上绘制,甚至是改变文本的布局和影响行高这样的元素。Span是

作用 xmlns:tools="http://schemas.android.com/tools"