Issue
I have a fragment which opens a new activity. From this activity, once the save or back button is pressed, the data in the activity is retrieved and sent back to the fragment. This is atleast, what I expect to happen. On devices where it doesnt work as expected, the new activity launches, but when returning to the fragment, the function that recieves the data, onActivityResult
from ActivityResultCallback
, doesn't get called. Additionally, the fragment that is shown when coming back is not the correct one. This leads me to think that the activity the the fragment is in isn't properly saved and is showing the default/first fragment. This isn't an error in the application, so there aren't any error messages.
At first I was using the deprecated onActivityResult
from the fragment class, and tried changing that to the newer method which is using the onActivityResult method from ActivityResultCallback
. However the problem still exists, So I doubt that this is a problem due to the API version. These are the devices I tested and which ones acted as I expected
Phone | How it ran |
---|---|
Pixel - API 28 - Emulator | As expected |
Pixel 2 - API 29 - Emulator | As expected |
Moto x4 - API 28 - Real | As expected |
Moto g7 Power - API 29 - Real | Not As expected |
I have check google's documentation for the ActivityResultLauncher
and am not able to understand what I am doing wrong.
Here are the snippets of my code that apply to this problem.
I have a class called Intent
which I have used in this project which is similar to the android.content.Intent
name. In these snippets I have used android.content.Intent
when referring to android.content.Intent
and Intent
for my Intent
class.
PlanFragment.java
public class PlanFragment extends Fragment {
...
ActivityResultLauncher<android.content.Intent> mStartForResult = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
Log.d("Cursor", "OnActivityResult");
if (result.getResultCode() == Activity.RESULT_OK) {
Log.d("Cursor", "OnActivityResultOk");
}
}
});
...
public View onCreateView(...) {
...
FloatingActionButton addIntent = view.findViewById(R.id.add_intent);
addIntent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
android.content.Intent intent = new android.content.Intent(view.getContext(), AddEditIntentActivity.class);
mStartForResult.launch(intent);
}
});
...
}
...
}
AddEditIntentActivity.java
public class AddEditIntentActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
...
private void saveIntent() {
Intent data = new Intent();
data.putExtra(EXTRA_NAME, name);
data.putExtra(EXTRA_DESC, desc);
data.putExtra(EXTRA_IMAGE, image.trim());
data.putExtra(EXTRA_TIME, time);
int id = getIntent().getIntExtra(EXTRA_ID, -1);
if (id != -1) {
data.putExtra(EXTRA_ID, id);
}
Log.d("Cursor", "SaveIntent");
setResult(Activity.RESULT_OK, data);
Log.d("Cursor", "SetResult");
onBackPressed();
Log.d("Cursor", "backPressed");
}
...
}
I tried replacing the onBackPressed()
with finish()
, but that didn't help either.
The only Log messages that appear are SaveIntent
, SetResult
, and backPressed
. OnActivityResult
and OnActivityResultOk
only appear on devices where it works.
Can someone help me identify why this problem is occuring and how I can fix it?
Solution
Using Log messages I realized that the activity/fragments were being recreated everytime. I then thought of checking my developer options to see if Don't keep activities was on. And it turns out it was. After toggling that, It started working again!
Answered By - Amruth Arunkumar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.