In Android development, how does one redirect external Activity classes to specified fragment fragments?

Source: Internet
Author: User

In Android development, how does one redirect external Activity classes to specified fragment fragments?

Let's talk about the background of this problem.

Activity A has four fragment types, namely a B c d. fragment a is displayed by default. During the development process, fragment d needs to jump to the external Activity and name this external Activity B...

I need to jump from d to B, complete some settings, and then return to d from the return button in B. However, the result of startActivity () method is that the jump from d to B can be realized, but a problem occurs when the jump from B to d.

I searched for solutions to related problems on the Internet (blog)

Solution 1:

To reuse fragment, we must reduce the coupling between fragment and activity, that is, fragment should not directly affect other fragment, nor be directly associated with other external activities. The activity where fragment is located should act as an intermediate bridge and be responsible for connecting with external activities. The blogger provides two solutions. The following is 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;/*** callback for setting Button click * @ author zhy **/public interface FOneBtnClickListener {void onFOneBtnClick ();} @ 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;}/*** to the host Activity for processing. if it wants 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; private FTwoBtnClickListener fTwoBtnClickListener; public interface FTwoBtnClickListener {void onFTwoBtnClick ();} // set the callback interface public void setfTwoBtnClickListener (FTwoBtnClickListener fTwoBtnClickListener) {this. fTwoBtnClickListener = fTwoBtnClickListener;} @ Overridepublic View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view View = inflater. inflate (R. layout. fragment_two, container, false); mBtn = (Button) view. findViewById (R. id. id_f Ragment_two_btn); mBtn. setOnClickListener (this); return view ;}@ Overridepublic void onClick (View v) {if (fTwoBtnClickListener! = Null) {fTwoBtnClickListener. onFTwoBtnClick ();}}}
Below is 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. listener; public class MainActivity extends Activity implements FOneBtnClickListener, listener {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 ();}/*** call back when the FragmentOne button is clicked */@ Overridepublic void onFOneBtnClick () {if (mFTwo = null) {mFTwo = new FragmentTwo (); mFTwo. setfTwoBtnClickListener (this);} FragmentManager fm = getFragmentManager (); FragmentTransaction tx = fm. beginTransaction (); tx. replace (R. id. id_content, mFTwo, "TWO"); tx. addToBackStack (null); tx. commit ();}/*** call back when the FragmentTwo button is clicked */@ 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 ();}}
Link to the original article

Solution 1 is the clearest, but not suitable for projects I am working on, because the modules I am in charge of do not include the activity where fragment I wrote is located, I cannot modify that activity... so this solution is passed


Solution 2:

Similar to solution 1, intent append parameters are used to determine the additional intent parameters in the activity in which fragment is located, so as to set which fragment to display. This is simpler and clearer than solution 1. The general idea is as follows:

You can add a parameter to intent in your startactivity, for example, intert. putExtra ("fragid", 1); then, you can obtain this id from the oncreate or Onresume function in the main activity of your fragment, for example, int id = intert. getIntegerExtra ("fragid",-1 );

If (id> 0 ){

If (id = target id) myfragment. setvisible (true );

}

Use a similar method.

It is relatively simple, but it does not apply to me either. The reason is the same as the Case 1.


Solution 3:

I checked the lifecycle of Fragment and acitment, and I have no clue. Suddenly I saw the startActivityForResult () method, combined with other keywords for a search, and found related things. below is the original article.

Recently, in a project, we encountered a situation where startactivityResult () was called when a new activity was started in fragment (). We all know that after the newly created activity is executed, the onResultActivity () function of the fragment or fragment activity will be executed. But who is it? Or who will execute it first?

First, it should be clear that the onResultActivity () function can also be defined in fragment. Let's first redefine this scenario. Activity A creates fragment B and uses the startactivityResult () function in fragment B to jump to activity C.

First, the onResultActivity () function in activity C will not be executed.

Secondly, the onResultActivity () function in activity A will be executed first, whether you use getactivity (). onResultActivity () in fragment B or use onResultActivity () directly ().

Besides, if you overwrite the onActivityResult method of activity
Without super. onActiivtyResult (), your onActivityResult in fragment B will not be called. Because super. onActiivtyResult () will go to the fragment in the current activity A and call the onActiivtyResult of fragment.

In this solution, you only need to modify my fragment d and the redirected Activity B, and do not need to modify Activity A, which meets my needs ~~~


Record.


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.