'Returning activityLauncher result from activity to activity to fragment in Android Studio
Good morning lads, Got myself quite the confusing problem, atleast for myself:
I have a fragment (page 1) which contains a button that opens an activity (page 2) using activityLauncher so it returns a result, and within that same activity, a button for the exact same purposes (page 3).
profile_fragment.java (page 1) + including the intent launch
ActivityResultLauncher<Intent> activityLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == 111) {
Intent intent = result.getData();
if (intent != null) {
item = (ExerciseItemFinal) intent.getSerializableExtra("siu");
Toast.makeText(getContext(), item.getName(), Toast.LENGTH_SHORT).show();
}
}
}
}
);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ExerciseCategoryChooser.class);
activityLauncher.launch(intent);
}
});
ExerciseCategoryChooser.java (page 2) including the intent launch
ActivityResultLauncher<Intent> activityLauncher2 = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == 011) {
Intent intent = result.getData();
if (intent != null) {
item = (ExerciseItemFinal) intent.getSerializableExtra("code123");
Intent i = new Intent();
i.putExtra("siu", item);
setResult(111, intent);
finish();
}
}
}
}
);
@Override
public void onItemClick(int position) {
Intent intent = new Intent(ExerciseCategoryChooser.this, ConfirmAdditionExercise.class);
activityLauncher2.launch(intent);
}
ConfirmAdditionExercise.java (page 3)
bSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ExerciseItemFinal item = new ExerciseItemFinal("Siuu", "10.5", "5");
Intent intent = new Intent();
intent.putExtra("code123", item);
setResult(011, intent);
finish();
}
});
The problem with my code is that I am able to return the result of page 3 to page 2, so inside of page 2 I can use the object to do whatever I need, but returning the same object from page 2 to page 1, just won't work for some reason, whenever I try to use "item.getName()" it gives me a NullPointerException (which doesn't happen on the 2nd page):
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.poop123, PID: 23789
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.poop123.ExerciseItemFinal.getName()' on a null object reference
at com.example.poop123.profile_fragment$1.onActivityResult(profile_fragment.java:66)
at com.example.poop123.profile_fragment$1.onActivityResult(profile_fragment.java:57)
at androidx.activity.result.ActivityResultRegistry$1.onStateChanged(ActivityResultRegistry.java:148)
at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)
at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:265)
at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:307)
at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:148)
at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:134)
at androidx.fragment.app.Fragment.performStart(Fragment.java:3026)
at androidx.fragment.app.FragmentStateManager.start(FragmentStateManager.java:589)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:300)
at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1647)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3128)
at androidx.fragment.app.FragmentManager.dispatchStart(FragmentManager.java:3079)
at androidx.fragment.app.FragmentController.dispatchStart(FragmentController.java:262)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:510)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1433)
at android.app.Activity.performStart(Activity.java:7923)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3332)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2044)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7562)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
I have all classes using Implements Serializable even if they don't require, and most confusing of all, I have a separate process for a different fragment which is basically the same thing, the only thing changing is the type of object being passed, but working.
I would be very embarressed if the solution is simple and obvious...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
