0

I have two activities: Activity A and Activity B.

Activity A shows Activity B via an Intent.

Activity A displays a number of fragments, each of which may display Activity B using said Intent.

Let's say I have Fragments X, Y & Z and when Activity B is shown from Fragment Y then I want to be run some code when Activity B is finally showing. The code I run will also need a reference to Activity A.

Can this be done?

Gerard
  • 4,818
  • 5
  • 51
  • 80

4 Answers4

2

It is bad idea.

If you run new activity, Activity B, then previous activity (Activity A) and it's fragments are already saved their's states. Their's onSaveInstance and onPause methods are already called and you should not modify any views in activity/fragment after onSaveInstance. If you modify views after onSaveInstance , those changes will not get saved to saved state. Android can destroy activity any time after onPause and restore it later from saved state.

If you don't need to change any views of fragment, consider passing some data into Activity B and doing something in it.

For more details on activity destroying/restoring read here https://developer.android.com/training/basics/activity-lifecycle/recreating.html

babay
  • 4,689
  • 1
  • 26
  • 40
  • I absolutely need to fire a callback though. Do you recommend using a new fragment instead of Activity? – Gerard Jun 25 '16 at 18:14
  • yes, using new fragment instead of Activity is a good way to solve that. Consider calling callback to actyvity and delivering message to old fragment from activity. It might be more complex, but I prefer to do all interaction between fragments through activity. – babay Jun 26 '16 at 01:34
  • You can call "startActivityForResult" to call an activity and then pass some result back to calling activity. – babay Mar 29 '23 at 18:27
1

For your use case, make Activity B a fragment. This way you can access activity A from fragment B through getActivity().

user3215142
  • 326
  • 2
  • 6
0

I think peeps overcomplicated this. Just have some callback that tells your activity when to start Activity B from Fragments X, Y, or Z. Maybe do it in onAttach.

Ex:

Fragment X

private MyCallback _myCallback;
@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof MyCallback){
        _myCallback =(MyCallback) context;
    }
}

Now whenever something happens that you need to start Activity B, just do:

// Still in Fragment X
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("bloop", "frag_x"); // or "frag_y", "frag_z"
_myCallback.startActivityB(intent)

Now in Activity B just check the value of bloop in the arguments and BAM. You know where it came from. I intentionally left some things out as an exercise. You can definitely make this more maintainable. Maybe use those OO skills to not duplicate the code that grabs that callback, etc.

Andy
  • 10,553
  • 21
  • 75
  • 125
-1

Common method to resolve the problem is via using callback. But there is major drawback with callback. You need to putting callback code here and there and ending tight coupling your codes. And then you've got a more problem just to make sure you have the 'right' callback.

My suggestion is to use EventBus system to decouple your code.

You just need to register Activity to receive Event.

Here a scenario with your case:

  1. Fragment from Activity A launch Activity B called via intent.
  2. Activity B has shown, inform Activity A. Send MessageEvent that Activity B has shown.

in Activity B onCreated:

EventBus.getDefault.post(new ActivityBHasShownEvent());

in Activity A, receive the message:

@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().register(this); // Subscribe to event.
...
}

@Subscribe public void onMessageEvent(ActivityBHasShownEvent event) {
   //Do something after activity B shown
}
  1. Done.
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96