Android开发-Activity和Fragment传值相关(Activity传值给Fragment)

简介:

Activity经常需要向Fragment进行数据传递,推荐使用fragment.newInstance的方式进行数据传递,以保证界面切换的时候数据不会丢失。

参考阅读:Android开发-Fragment和Activity传值相关(Fragment传值给Activity)

 

步骤一:MainActivity.java文件

package com.huwan.activity2fragmentvaluedemo2;

 

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

 

public class MainActivity extends AppCompatActivity {

 

    // 1.定义了几个字符串

    private String aaaa = "字母a值";

    private String bbbb = "字母b值";

    private String cccc = "字母c值";

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        // 4.点击进行fragment切换

        Button btn1;

        Button btn2;

        Button btn3;

        btn1 = findViewById(R.id.btn1);

        btn1.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, Fragment1.newInstance(aaaa, bbbb)).commit();

            }

        });

        btn2 = findViewById(R.id.btn2);

        btn2.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, Fragment1.newInstance(bbbb, cccc)).commit();

            }

        });

        btn3 = findViewById(R.id.btn3);

        btn3.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, Fragment1.newInstance(aaaa, cccc)).commit();

            }

        });

 

        // 2.activity的数据可能是传递或者获取的,所以一定要进行判断

        if (savedInstanceState != null) {

            aaaa = savedInstanceState.getString("字母a值");

            bbbb = savedInstanceState.getString("字母b值");

            cccc = savedInstanceState.getString("字母c值");

        }

    }

 

    // 3.防止数据丢失,一定要进行保存

    @Override

    protected void onSaveInstanceState(Bundle outState) {

        super.onSaveInstanceState(outState);

        outState.putString("字母a值", aaaa);

        outState.putString("字母b值", bbbb);

        outState.putString("字母c值", cccc);

    }

}

 

步骤二:Fragment1.java文件

package com.huwan.activity2fragmentvaluedemo2;

 

import android.app.Activity;

import android.support.v4.app.Fragment;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.TextView;

 

public class Fragment1 extends Fragment {

 

         //5.获取传递过来的值,并保存在bundle中

         String zhi1;

         String zhi2;

         public static Fragment1 newInstance(String zhi1,String zhi2) {

                   Fragment1 newFragment = new Fragment1();

                   Bundle bundle = new Bundle();

                   bundle.putString("zhi1", zhi1);

                   bundle.putString("zhi2", zhi2);

                   newFragment.setArguments(bundle);

                   return newFragment;

         }

 

         @Override

         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

                   View view = inflater.inflate(R.layout.fragment1, container, false);

                   //6.将保存的值取出,进行展示

                   Bundle args = getArguments();

                   if (args != null){

                            zhi1 = args.getString("zhi1");

                            zhi2 = args.getString("zhi2");

 

                            TextView text1 = view.findViewById(R.id.text1);

                            text1.setText(zhi1);

                            TextView text2 = view.findViewById(R.id.text2);

                            text2.setText(zhi2);

                   }

                   return view;

         }

 

}

 

步骤三:其它相关文件

activity_main.xml

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

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">

 

   

        android:id="@+id/btn1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textColor="@color/colorAccent"

        android:textSize="20dp"

        android:text="切换一" />

 

   

        android:id="@+id/btn2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textColor="@color/colorAccent"

        android:textSize="20dp"

        android:text="切换二" />

 

   

        android:id="@+id/btn3"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textColor="@color/colorAccent"

        android:textSize="20dp"

        android:text="切换三" />

 

 

   

        android:id="@+id/content_frame"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:background="#ff0000"

        android:fitsSystemWindows="true" />

 

----------------------------------------------------------------

fragment1.xml

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

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:background="@color/colorAccent">

   

        android:id="@+id/text1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textSize="20dp"

        android:text="值一" />

   

        android:id="@+id/text2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textSize="20dp"

        android:text="值二"/>

 

Demo样例下载:

http://www.huwan.xin/Source/AppPackage/ZIP/Android/ Activity2FragmentValueDemo2.rar

更多内容请访问:www.huwan.xin

 

你可能感兴趣的:(Fragment)