Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Android jumps from Fragment to other Activity

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Android how to jump from Fragment to other Activity, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

To better understand the following, we need to take a brief look at the dynamic registration method of Fragment

Static and dynamic Registration of Android--Fragment

In order to jump from Fragment to another Activity, the following files need to be created:

Step 1: simply write the layout file fragment_activity.xml and the abstract class TemplateFragmentActivity.java code as follows:

Fragment_activity.xml

Fragment_activity.xml layouts are mainly used to host fragment layouts, such as fragment_one.xml and fragment_two.xml.

TemplateFragmentActivity.java

Package com.example.myapplication;import android.os.Bundle;import androidx.annotation.Nullable;import androidx.appcompat.app.AppCompatActivity;import androidx.fragment.app.Fragment;import androidx.fragment.app.FragmentManager;import androidx.fragment.app.FragmentTransaction;public abstract class TemplateFragmentActivity extends AppCompatActivity {private FragmentManager fm; private FragmentTransaction ts; private Fragment fragment; / / abstract method to create a Fragment instance protected abstract Fragment createFragment (); @ Override protected void onCreate (@ Nullable Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.fragment_activity) Fm = getSupportFragmentManager (); ts = fm.beginTransaction (); if (fragment = = null) {fragment = createFragment (); ts.add (R.id.temp fragment fragmentation activity fragment); ts.commit ();}

Step 2: make class FragmentOneActivity and FragmentTwoActivity inherit class TemplateFragmentActivity and implement abstract method createFragment (), respectively.

FragmentOneActivity.java

Package com.example.myapplication;import androidx.fragment.app.Fragment;public class FragmentOneActivity extends TemplateFragmentActivity {@ Override protected Fragment createFragment () {return new FragmentOne ();}}

FragmentTwoActivity.java is similar to FragmentOneActivity.java and is not repeated. The third step: write fragment_one.xml and fragment_two.xml layout files respectively and bind FragmentOne.java and FragmentTwo.java to the corresponding layout files, and achieve their specific functions.

Fragment_one.xml

Fragment_two.xml is similar to fragment_one.xml and is not repeated.

FragmentOne.java

Package com.example.myapplication;import android.content.Intent;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import androidx.annotation.NonNull;import androidx.annotation.Nullable;import androidx.fragment.app.Fragment;public class FragmentOne extends Fragment {private Button mBtnFragmentOne; @ Nullable @ Override public View onCreateView (@ NonNull LayoutInflater inflater, @ Nullable ViewGroup container, @ Nullable Bundle savedInstanceState) {View view = inflater.inflate MBtnFragmentOne = view.findViewById (R.id.btn_fm_one); mBtnFragmentOne.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (getActivity (), FragmentTwoActivity.class); startActivity (intent);}}); return view;}}

The Fragment jump to Activity is similar to the Activity jump to Activity method, as follows:

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

FragmentTwo.java is similar to FragmentOne .java and is not repeated.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report