Reputation: 143
I have a Main Activity that extends to AppCompatActivity, which contains a FrameLayout and I used to replace it with the desire Fragment.
However, at one time, I called another activity only to show up some specific information, and need to pass back that information to the Main Activity with the last Fragment showed. Thus, I tried to use FragmentManager, and the Fragment shows transparent; also, if I used that, I lost the Main Activity's FrameLayout id.
To try to clarify, I will put a scheme:
Is possible to use onBackPressed to solve it?
UPDATE
Below is my onActivityResult that calls startActivityForResult, which is used in a Fragment.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2) {
if (resultCode == Activity.RESULT_OK) {
execusaoVistoria.fotos = allFotos;
Intent startPreview = new Intent(getContext(), PreviewActivity.class);
startPreview.putExtra("path", fileName.toString());
startPreview.putExtra("ExecusaoVistoria", execusaoVistoria);
startActivityForResult(startPreview, 10);
}
} else if (requestCode == 10) {
if (resultCode == Activity.RESULT_OK) {
allFotos.add((Fotos) data.getBundleExtra("Bundle").getParcelable("foto"));
}
}
}
And here, is the code from the Activity that calls the setResult:
Intent i = new Intent();
i.putExtra("Bundle", bundle);
setResult(Activity.RESULT_OK, i);
finish();
Upvotes: 1
Views: 998
Reputation: 25
try out using intent in fragment as Intent i=new Intent(getActivity(), anotherActivity.class); startActivityForResult(i);
and for back process, declare parent activity as your main activity in manifest.
Upvotes: 0
Reputation: 148
There are 2 solutions to what you can do based on what you want to achieve with it. You can open the second Activity with startActivityForResult
from inside your fragment and handle the result in your fragment's onActivityResult
method.
The second is to handle the result from inside your activity using the same methods of the activity.
In both cases what you are called to do is to call startActivityForResult(yourIntent, yourRequestCode)
to open the requested activity. In the activity that was opened, when you are finished and have gotten the result you have to call
Intent resultIntent = new Intent();
//in case you do not need to pass data back you can skip this
resultIntent.setExtra("extra_result", extraData);
setResult(resultCode,yourResultIntent);
finish();
When this returns you to the previous activity the onActivityResult(resultCode,requestCode)
method will be called with the request code you specified when starting the activity and the result code you set when calling setResult
.
In order to handle the result inside the fragment you need to override the onActivityResult
method in your Activity and call through to the super.onActivityResult
and also override the method inside the fragment. In this case you will handle the result there.
The thing that you need to watch out for is to call the startActivityForResult
of the actual handler of the result.
If you want to see a full sample of the basic interaction you can check it at https://developer.android.com/training/basics/intents/result.html
Upvotes: 0
Reputation: 3026
You should use startActivityForResult instead of startActivity.
In your MainActivity you will receive a callback
onActivityResult()
here you can fetch the info you need.
How to manage `startActivityForResult` on Android?
Upvotes: 4