Hello! : ) There is no ideal solution for this type of problem, at most one can identify names, for example, you can find out the name of Activity that called another Activity through the method getCallingActivity
.
A solution unsaved would be to use a static method in Activity and pass Fragment to a static variable.
But most likely you are wanting to do something that there is a better way to do, if you want to pass information to the Fragment that called Activity and identify the moment that Activity is terminated, you should call this Activity with the method startActivityForResult
and on Activity before closing it you should pass the information by intent
with the method setResult
Example:
In the Fragment:
Intent i = new Intent(this, MinhaActivity.class);
getActivity().startActivityForResult(i, 1);
In the called Activity (Minhaactivity):
Intent retornoIntent = new Intent();
retornoIntent.putExtra("resultadoid",resultado);
setResult(Activity.RESULT_OK,retornoIntent);
finish();
And in the event that called Minhaactivity:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String resultado = data.getStringExtra("resultadoid");
//Momento exato quando a activity chamada por esse fragment é
//encerrada e o estado do app volta para esse fragment
}
}
}
That is, as there is no good reason to call a method from a Fragment that is not being used (because the current state of the app is in an Activity after the Fragment), we should not call a method from somewhere in the app that does not belong to the current state, instead, we should identify the exact time we returned to the previous state and when necessary use the necessary information of the state that has just been closed
Observing
It is very important to show here that Fragment will not have arrived at the method onActivityResult
without Activity possessing that Fragment calling onActivityResult
of this Fragment. Example assuming that the Fragment is inside Activitya:
Example - Activitya:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
fragmentA.onActivityResult(requestCode,resultCode,data);
//É necessário fazer isso para que o fragmentA tenha seu método onActivityResult chamado
}
Hugs :)
The method is the same or an example. The solution will depend on what he does.
– ramaral
In this case it is just an example, but the method has to send a string to the Fragment that called Activity.
– Daniel Andre Carlos Candido