Transfer of values between Activity and Activity,Activity and Fragment,Fragment and Fragment

Posted by TheHyipSite on Mon, 06 Jan 2020 07:35:23 +0100

1, Value passing between Activity and Activity

First create two activities, one is MainActivity and the other is SecondActivity

Then add a Button in activity main.xml, the code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:text="ToSecondActivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/toSecond"
        android:textAllCaps="false"/>

</LinearLayout>

Add a TextView to activity_second.xml. The code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SecondActivity">



    <TextView
        android:id="@+id/showData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Then initialize the corresponding control in MainActivity and SecondActivity respectively

MainActivity code is as follows:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private Button toSecond;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

    }

    private void initView() {

        toSecond = (Button)findViewById(R.id.toSecond);

        toSecond.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        
        switch (v.getId()){
            case R.id.toSecond:
                //Jump logic
                break;
        }

    }
}

The code of SecondActivity is as follows:

public class SecondActivity extends AppCompatActivity {

    private TextView showData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        initView();

        initData();

    }

    private void initView() {

        showData = (TextView)findViewById(R.id.showData);

    }

    private void initData(){

       //TODo
    }
}

//The first way: through setClass method

Add the following code to the click of main activity:

case R.id.toSecond:
    //Jump logic
    Intent intent = new Intent(this,SecondActivity.class);
    intent.putExtra("name","A small piece of rubbish");
    intent.putExtra("age","21");
    startActivity(intent);
    break;

In the second activity

Add the following code to the initData() method:

Intent intent = getIntent();
String name = intent.getStringExtra("name");
String age = intent.getStringExtra("age");
showData.setText("Full name:" + name + ";" + "Age:" + age);

Then run it. Let's see the effect:

The second is through Bundle:

Modify the case event code in MainActiviy as follows:

case R.id.toSecond:
    //Jump logic
    Intent intent = new Intent(this,SecondActivity.class);
    Bundle bundle = new Bundle();
    bundle.putString("name","A small piece of rubbish");
    bundle.putString("age","21");
    intent.putExtra("bundle",bundle);
    startActivity(intent);
    break;

Then modify the code in initData() method in SecondActivity to:

private void initData() {

    Intent intent = getIntent();
    Bundle bundleExtra = intent.getBundleExtra("bundle");
    String name = bundleExtra.getString("name");
    String age = bundleExtra.getString("age");
    showData.setText("Full name:" + name + ";" + "Age:" + age);
}

Let's see the operation effect:

We can see that the running effect is the same. Then someone has to ask, what's the difference between intent and bundle. Here will not repeat, interested Baidu on the line.

2, Value passing between Activity and Fragment

Create a new FirstFragment class, inherit from Fragment. And override the onCreateView method

Create a new fragment ﹐ fireset.xml layout file.

Code directly.

Using Bundle to transfer values

MainActivity full code:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private Button toSecond;
    private FragmentManager fm;
    private FragmentTransaction transaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        initView();


    }

    private void initView() {

        toSecond = (Button) findViewById(R.id.toSecond);

        toSecond.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        fm = getSupportFragmentManager();
        transaction = fm.beginTransaction();

        switch (v.getId()) {
            case R.id.toSecond:
                //Jump logic
                FirstFragment firstFragment = new FirstFragment();
                Bundle bundle = new Bundle();
                bundle.putString("name", "A small piece of rubbish");
                bundle.putString("age", "22");
                firstFragment.setArguments(bundle);
                transaction.add(R.id.fragment, firstFragment);
                transaction.addToBackStack(null);
                break;
        }
        transaction.commit();
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:text="ToSecondActivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/toSecond"
        android:textAllCaps="false"/>

    <FrameLayout
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="300dp"></FrameLayout>

</LinearLayout>

FirstFragment Code:

public class FirstFragment extends Fragment {

    private View view;
    private TextView fragment_showData;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

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

        initView();

        initData();

        return view;
    }

    private void initData() {
        Bundle bundle = getArguments();
        String name = bundle.getString("name");
        String age = bundle.getString("age");
        fragment_showData.setText("name:" + name + ";"+ "age:" + age +"year");
    }

    private void initView() {

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

    }
}

fragment_first.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF0">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fragment_showData"/>

</LinearLayout>

Effect:

2, Transfer of values between Fragment and Fragment

The value transfer between fragment and fragment is divided into Bundle value transfer and value transfer between interface callbacks. Because the length of the blog is too long, please move to my blog.

Topics: Android Fragment xml encoding