How to implement external other activity classes with specified fragment fragments in Android development

Source: Internet
Author: User

Let's talk about the background of the problem.

Activity A has four fragment, a B c D, and fragment A is shown by default. During the development process, fragment D needs to jump with the external activity and name the external activity b ...

I need to jump from D to B, complete some settings, and then return to D from the back button in B. But the result of using the StartActivity () method is that jumps from D to B can be implemented, but there is a problem with jumping from B to D.

Search online for solutions to related problems (blog post)

Programme one:

To consider the reuse of fragment, it is necessary to reduce the coupling between fragment and activity, that is to say fragment should not directly affect other fragment, and should not be directly associated with other external activity. The activity in which the fragment is located should act as an intermediary bridge, responsible for contacting external activity. The blogger gave two solutions. Here's his code.

Fragmentone

Package Com.zhy.zhy_fragments;import Android.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;public class Fragmentone extends Fragment implements Onclicklistener{private button mbtn;/** * Set buttons click on Callback * @author Zhy * */public interface fonebtnclicklistener{void ONFONEBTN Click ();} @Overridepublic View Oncreateview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {View view = Inflater.inflate (R.layout.fragment_one, container, false); mBtn = (Button) View.findviewbyid (r.id.id_fragment_one_ BTN); Mbtn.setonclicklistener (this); return view;} /** * gives the host activity to handle if it wishes to process */@Overridepublic void OnClick (View v) {if (getactivity () instanceof Fonebtnclicklistener) { ((Fonebtnclicklistener) getactivity ()). Onfonebtnclick ();}}
Fragmenttwo

Package Com.zhy.zhy_fragments;import Android.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;public class Fragmenttwo extends Fragment implements Onclicklistener{private Button mBtn;p rivate ftwobtnclicklistener ftwobtnclicklistener;p ublic interface Ftwobtnclicklistener{void Onftwobtnclick ();} Set callback interface public void Setftwobtnclicklistener (Ftwobtnclicklistener ftwobtnclicklistener) {This.ftwobtnclicklistener = Ftwobtnclicklistener;} @Overridepublic View Oncreateview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {View view = Inflater.inflate (R.layout.fragment_two, container, false); mBtn = (Button) View.findviewbyid (r.id.id_fragment_two_ BTN); Mbtn.setonclicklistener (this); return view; } @Overridepublic void OnClick (View v) {if (Ftwobtnclicklistener! = null) {Ftwobtnclicklistener.onftwobtnclick ();}}}
Here are the activity's

Package Com.zhy.zhy_fragments;import Android.app.activity;import Android.app.fragmentmanager;import Android.app.fragmenttransaction;import android.os.bundle;import android.view.window;import com.zhy.zhy_fragments. Fragmentone.fonebtnclicklistener;import com.zhy.zhy_fragments. Fragmenttwo.ftwobtnclicklistener;public class Mainactivity extends Activity implements Fonebtnclicklistener, Ftwobtnclicklistener{private fragmentone mfone;private fragmenttwo mftwo;private FragmentThree mFThree;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (r.layout.activity_main); mFOne = new FragmentOne (); Fragmentmanager fm = Getfragmentmanager (); Fragmenttransaction tx = Fm.begintransaction (); Tx.add (R.id.id_content, Mfone, "one"); Tx.commit ();} /** * Fragmentone button click Callback */@Overridepublic void Onfonebtnclick () {if (mftwo = = null) {mftwo = new fragmenttwo (); mftwo.setf Twobtnclicklistener (this);} Fragmentmanager FM= Getfragmentmanager (); Fragmenttransaction tx = Fm.begintransaction (); Tx.replace (R.id.id_content, Mftwo, "a"); Tx.addtobackstack (null); Tx.commit ();} /** * Fragmenttwo button click Callback */@Overridepublic void Onftwobtnclick () {if (Mfthree = = null) {Mfthree = new Fragmentthree ();} Fragmentmanager fm = Getfragmentmanager (); Fragmenttransaction tx = Fm.begintransaction (); Tx.hide (mftwo); Tx.add (R.id.id_content, Mfthree, "THREE");// Tx.replace (R.id.id_content, Fthree, "three"); Tx.addtobackstack (null); Tx.commit ();}}
Attach the original link

The approach I think is the clearest, but not suitable for the project I am doing now, because the module I am responsible for does not include the activity that I wrote fragment, I can't modify that activity ... So this program was pass


Scenario Two:

Similar to the scheme, the use of intent additional parameters, the fragment in the activity of the intent to determine the additional parameters, so as to set the display of which fragment, this is more straightforward than the scheme. Probably the following ideas

You can add a parameter to the intent in your startactivity, for example, Intert.putextra ("Fragid", 1); Then you get this ID in the OnCreate or Onresume function in your fragment's main activity, such as int id = Intert.getintegerextra ("Fragid",-1), and then jump to it based on this ID.

if (Id > 0) {

if (id = = Target ID) myfragment.setvisible (TRUE);

}

In a similar way you can

It is relatively simple, but it does not apply to my situation, for the same reason as programme I.


Programme III:

I checked the life cycle of fragment and acitvity, and I had no clue. Suddenly see Startactivityforresult () This method, combined with other keywords a search, also really found the relevant things, the following is the original text.

Recently, in a project, a situation was encountered in which fragment called Startactivityresult () when a new activity was opened. We all know that after executing that newly created activity, we will come back to perform the onresultactivity () function of the activity where fragment or fragment is located. But who is it that executes it? Or who to execute first?

The first thing to be clear is that in fragment, you can also define the onresultactivity () function. Let's start by redefining this scenario. Activity A creates fragment B and jumps to activity C with the Startactivityresult () function in fragment B.

First, the onresultactivity () function in activity C is definitely not executed.

second, The first step is to execute the onresultactivity () function in activity A, regardless of the getactivity () that you used in fragment B. onresultactivity (), or direct use of onresultactivity ().

< Span style= "font-family:monospace; font-size:14px; White-space:pre-wrap "> Span style= "font-family:monospace; font-size:14px; White-space:pre-wrap "> If you rewrite the Onactivityresult method of activity a <BR>

This program only needs to modify my fragment D and jump activity B, do not need to modify activity A, to meet my needs ~ ~ ~


Recording.


How to implement external other activity classes with specified fragment fragments in Android development

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.