logo
Tags down

shadow

How to save fragment state while navigating with navigation component


By : Volodymyr Basyuk
Date : September 17 2020, 09:00 AM
around this issue I'm trying to create a single activity app using android architecture components. I have a fragment A which has some textfields, when user pushes a button I navigate to fragment B where he uploads and edits some images after that app navigates back to A using code like this: , I will answer your questions one by one.
code :
findNavController().navigate(R.id.action_from_B_to_A, dataBundle)
FragmentA (new instance)
FragmentB
FragmentA (old instance)
class SharedViewModel : ViewModel() {

    // This is the data bundle from fragment B to A
    val bundleFromFragmentBToFragmentA = MutableLiveData<Bundle>()
}
private lateinit var viewModel: SharedViewModel

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    viewModel = ViewModelProviders.of(requireActivity()).get(SharedViewModel::class.java)
    viewModel.bundleFromFragmentBToFragmentA.observe(viewLifecycleOwner, Observer {
        // This will execute when fragment B set data for `bundleFromFragmentBToFragmentA`
        // TODO: Write your logic here to handle data sent from FragmentB
        val message = it.getString("ARGUMENT_MESSAGE", "")
        Toast.makeText(requireActivity(), message, Toast.LENGTH_SHORT).show()
    })
}
// 1. Declare this as class's variable
private lateinit var viewModel: SharedViewModel

// 2. Use the following code when you want to return FragmentA           
// findNavController().navigate(R.id.action_from_B_to_A) // Do not use this one

// Set data for `bundleFromFragmentBToFragmentA`
val data = Bundle().apply { putString("ARGUMENT_MESSAGE", "Hello from FragmentB") }
viewModel.bundleFromFragmentBToFragmentA.value = data

// Pop itself from back stack to return FragmentA
requireActivity().onBackPressed()


Share : facebook icon twitter icon

How to save fragment state while navigating through activity


By : Eugene Afonin
Date : March 29 2020, 07:55 AM
With these it helps You might be restarting ActivityA from ActivityB via startActivity() instead of finishing ActivityB. If it is so, just finish your ActivityB. Then you will be on the last shown fragment in your ActivityA.
Or you can also use onSaveInstanceState method to keep trace of your Fragments. This answer can help you with this.
code :
<activity android:name=".ActivityA" android:launchMode="singleTop">

Navigating to preference fragment using navigation component


By : user1994864
Date : March 29 2020, 07:55 AM

Android navigation component: how save fragment state


By : Gulshan singh
Date : March 29 2020, 07:55 AM
will help you As per the open issue, Navigation does not directly support multiple back stacks - i.e., saving the state of stack B when you go back to B from A or C since Fragments do not support multiple back stacks.
As per this comment:

Maintaining the state of fragment when navigating to other fragment using Navgation Architechture component


By : user3052797
Date : March 29 2020, 07:55 AM
should help you out As per the open issue, Navigation does not directly support multiple back stacks - i.e., saving the state of stack B when you go back to B from A or C since Fragments do not support multiple back stacks.
As per this comment:

How can I save the state of a component after navigating to another component in Angular 8


By : Sai Shiva
Date : March 29 2020, 07:55 AM
will help you You can use services to remember states between routes because they are usually singletons. The state Behaviour Subject can store an object which contains any states you might want to remember for you so every time the component is initialised all you have to do is retrieve the current value by .getValue() from the state$ in the service. Also, when you update any state value you should remember to update the state stored in the service.
state.service.ts
code :
@Injectable({
  providedIn: "root"
})
export class StateService {
  state$ = new BehaviourSubject<any>(null);
}
export class CityComponent implements OnInit {
  constructor(private stateService: StateService) {}
  state: any;
  ngOnInit() {
    this.state = this.stateService.state$.getValue() || {};
  }

  updateFoo(val: any) {
    this.state.foo = val;
    this.stateService.state$.next(state);
  }
<p>{{state.foo}}</p>
Related Posts Related Posts :
  • MediaManager volume control for Xamarin.Forms
  • custom ArrayAdapter showing only one object
  • Add Eigen to android
  • How can i create new react native App using npx?
  • jks or pkcs12: which one should I use to sign the apk for Google Play Store?
  • Drawable images in List not displaying on Android Studio. (Kotlin)
  • Set screenOrientation to "portrait" in release build only
  • FlutterActivity MethodChannel and FlutterView
  • Make a Firebase Firestore collection available offline
  • Android Emulator Error - Failed to open ..../joshuaduong/..../qemu.conf, err: 2
  • Flutter - Rebuild parent after calling setState in grandchildren
  • Koin java.lang.NoClassDefFoundError: Failed resolution of: Lorg/koin/dsl/context/ParameterHolderKt
  • Netinfo.isConnected always returns true in Android
  • How can I increase OpenCV realtime detector app's fps?
  • Can a Flutter app be proposed on the Huawei AppGallery?
  • How to get current latitude and longitude and show in a toast?
  • How important is the Android package name?
  • Android keyboard hides input in web view
  • In recyclerView, it seems like one cardView takes whole list view
  • Flutter assign text onchange value to variable
  • How to scroll a ListView without using NestedScrollView?
  • How to open (audio) file from file manager using Flutter app
  • React native - When i lock the screen while API call loading, App process stopped
  • Flutter GoogleMap is blank after resuming from background
  • Difference between APK extension(OBB file) and app Bundle
  • Why is DAO defined as fun in RoomDatabase?
  • How Android localization works
  • Android: Cannot use TabLayout in my design even after migration to Android X
  • Android exclude view from screenshot
  • error in linear layout.vertical. what should be the exact line
  • cannot start activity background in android 10 [ android Q ]
  • Is it possible to restart Android App after calling ActivityManager.clearApplicationUserData()
  • How to handle SwipeRefreshLayout on refresh?
  • how to remove .0 or E in Result or stop crashes if using integer
  • Difference between file and classes in kotlin
  • Refreshing data showing on the screen from backend in my case
  • Can we use RelativeLayout or LinearLayout inside ConstraintLayout Android
  • MAC address in android phone
  • Firebase Scheduled notification not sending notification daily
  • Attach Debugger to Android process option is missing
  • Use tf-lite C++ API for both Android and iOS
  • How to sign Android App on another computer
  • kotlin.KotlinNullPointerException (no error message) while building APK using Jenkins
  • Is it possible to constrain two widgets inside a linear layout?
  • How to get ONLY numeric keyboard in React-Native?
  • How to set the background colour of a Popup menu?
  • WAP_PUSH_DELIVER Intent never sent by Android system
  • Location Tracking, Doze, AppStandby
  • How to eliminate white spaces at the bottom of the screen while using RecyclerView
  • How to return multiple responses from "doInBackground"
  • setSupportActionBar(toolbar); is not wworking
  • Change current tab from fragment in android
  • How to detect that a notification bundle/group was clicked in Android?
  • Attempt to invoke virtual method '.MutableLiveData .getLiveData(java.lang.String)' on a null object reference
  • Android Libgdx memory leak on creating Actors
  • How to prevent Google Cloud Storage redirect
  • Android app has stopped working: java.lang.ClassCastException
  • FlutterDriver: Use Android back button
  • use ExoPlayer in React Native
  • Failed to resolve: recyclerview
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk