How to know which Activity prior to the current Activity was called?

Asked

Viewed 678 times

7

I have several activities, which can call a special activity.

I’d like to know how to identify which Activity called.

It would be something like this:

inserir a descrição da imagem aqui

I need this because I have a user registration Activity, which can be called through various places in my application. But I need to know which place it was called from, because depending on who called it should perform an action.

I thought I’d do it like this:

Intent intent = new Intent(basecontext,Registro_Activity.class);
intent.putExtra("ACTIVITYPAI", "ACTIVITY-A");
startActivity(intent);

And put that on every screen you call. But if there’s a way that you don’t have to use the putExtra, and the actual record of identifying where it came from, it would be better for me.

  • I believe putExtra is less costly for this case. Unless you have a very special reason to discard this solution.

  • @Viana My reason for trying to avoid, is that I don’t know yet how many activities will call this same Action, I think it would be more practical for me, to know where it comes from and to do the verification only in what I need. So if I ever decide to change all this, I will change only in the Ctivity that is being called, and I will not need to modify all the others that call for it. Do you understand?? I do not know if I explained well kkk

1 answer

6


Using getCallingActivity():

If you start Activity with startActivityForResult, the method can be used getCallingActivity().getClassName():

private void startRegistroActivity() {
  Intent intent = new Intent(basecontext, Registro_Activity.class);
  startActivityForResult(intent, 100);
}

And on the second Activity:

@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  ...
  String className = getCallingActivity().getClassName();

}

Using Intents:

You can also use Intents, as you are already doing.

At first Activity:

Intent intent = new Intent(basecontext,Registro_Activity.class);
intent.putExtra("activity_name", this.getClass().getName());
startActivity(intent);

And in the second Activity:

@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  ...
  String className = getIntent().getStringExtra("activity_name");
}

Using Singletons or objects that live longer than Activity:

Another option is to use objects that live longer than Activity, such as Singletons.

Declare the Singleton:

public enum ActivityReference {

  INSTANCE;

  Class<?> callingActivity;

  public Class<?> getCallingActivity() {
    return callingActivity;
  }

  public void setCallingActivity(Class<?> callingActivity) {
    this.callingActivity = callingActivity;
  }
}

Keep the value in it:

ActivityReference.INSTANCE.setCallingActivity(this.getClass());
Intent intent = new Intent(this, Activity.class);
startActivity(intent);

Access it at a later date:

@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  ...
  Class<?> callingActivity = ActivityReference.INSTANCE.getCallingActivity();
}
  • Could you tell me how I could do through Singletons?? startActivityForResult maybe it works for me. I’ll try it this way you suggested, but I would like to know how it would be with Singleton too, if you can give me an example.

  • 1

    @Samantasilva added examples in the reply

  • Thank you, I haven’t tested yet to know which of the methods will be the best for me, but I love the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.