Retrieving an object when coming back to Activity - java

Here's the context of my question :
Context
I've made a Recyclerview that dysplay items thanks to an object called "identity_candle". When I click on items, it brings me to another activity with more description over the item. On this activity, there is a button to view more details about it that needs another layout by itself.
So when I click on the button it lands me on another activity.
IN SUMMARY, MainActivity (RecyclerVIew) => ItemActivity => "press button" => Activity or Fragment ? (Come back : Activity or Fragment => ItemActivity (keep previous object)
Problem
But when I want to come back to the previous activity it brings me error because the object state "identity_candle" is null.
Questions
What should I do ? Should I use fragment instead of activity when I press button ? (I've seen that object keeps their state between fragment and Activity but I'm not sure) Should I use SharedPreference ? If I want to create Fragment should I extends Fragment or ActivityFragment ?

This seems heavily rooted in understanding the Activity lifecyle. You'll likely want to go through that training if you haven't already. An understanding of onSaveInstanceState(), onRestoreInstanceState(), and Parcelables should get you there.

Related

Is it possible to prevent WebView or any View from being destroyed?

So my understanding is that View has different lifecycle then Fragment, I have noticed that by adding confingChanges to MainActivity android:name=".MainActivity" android:configChanges="orientation|screenSize|keyboardHidden", my WebView (or maybe the whole Fragment) is not being destroyed on rotation (or at least it seems like it is not being destroyed) i.e the Website is not being reloaded, so how does android does this? Is there any way to save the WebView when the fragment is being destroyed and then pass this webView to the fragment when it is being created?
I thought maybe following would work: detach the view in the onPause or any other method down the line (Fragments Lifecycle) save the View somewhere and then pass it back in the onCreateView method?
Background to the App: there is bottom navigation Bar which has 4 elements when one of these elements is clicked a new fragment is created and displayed inside of the main activity, while the Fragment before is being destroyed (this is achived using getSupportFragmentMenager().beginTransaction().replace().commit();), one of these Fragments has WebView in it, i would like to prevent the WebView from being destroyed?
I have achived the following: I am saving the state of the WebView inside onPause method and inside onSavedInstace method (for the cases when the activity itself gets destroyed), but the problem with this is that the Input that the user puts inside forms during the session but not sending them gets lost on reload/restore of the WebView, so would like to save the Webview so that even this remains when the user comes back to the site.

Question about BottomNavigationView in Android

I have general questions about BottomNavigationView. I would like to have a BottomNavigationView in each of my Activities in an App for ordering something (e.g. food). It should have 4 buttoms:
Back
Info
Stats
My Orders
With 'Back' the app should just go back to the previous activity. The buttoms 'Stats' and 'My Orders' should switch to a persistent activity that should not be destroyed when not being displayed. 'My Orders' should display the last orders. The buttom 'Info' should only display some information about the current item or current menu (depending from which activity it is called). So basically I have 2 questions:
Should the Activities 'Info', 'Stats', and 'My Orders' be real Activities or just Fragments? Normally I think that at leat 'Stats', and 'My Orders' should be real Activities as they are persistent. But in many BottomNavigationView only Fragments are used?
How can I pass content information to the Activity/Fragment 'Info'. This Activity/Fragment should display information based on the Activity is was called from. Let's say the Activities are different dishes. Do I have to create a separate Info-Activity/Fragment for each dish? Or can I somehow define a dynamic Activity/Fragment that displayes information based on the current Activity?
I'd appreciate every comment and I'd really appreciate your help.
The recommended approach is Single Activity and Multiple fragments.
You can do this using Jetpack's Navigation Component
In case you need to pass data from an Activity/Fragment to the new calling Fragment, it can be done by setting arguments on the calling fragment and then getting it on the called fragment. If there is something which requires to be dynamic, for example- dishes fragment, make a single fragment and common layout and load the data dynamically from the backend.
For Setting Arguments, this should help
How to pass a variable from Activity to Fragment, and pass it back?
Note: You can use fragment without using Navigation Components but you have to use FragmentManager and FragmentTransaction and also have to maintain the Backstack by yourself which could be quite complicated

Don't recycle old instance of same activity

I have a UserProfile Activity which is composed of different RecyclerView in tabs, there is a tab friends where i can click another user and a new instance of the same UserProfile will start.
The only problem is when i go press back from an instance: i've overrided the OnBackPressed which only contains finish() method, i will be redirected to the previous UserProfile instance, but all the RecyclerView and the profilePicture in the activity are still the same of the activity i've just finished.
I think it's due to the fact that they are RecyclerView but don't know how to solve it.

Android pushing new activity on top of TabActivity and coming back

I have a question that I've been looking for answer to for several days with no luck.
So, to be as specific, I have TabActivity inside TabActivity and listview inside it (I call this entire screen "main screen"). That listview is populated from data sent from my database. Upon clicking an item of the listview, I start an activity but upon exiting that activity and coming back to the main screen, the listview is recreated and I was wondering whether there was a way to stop from being recreated.
Which method do I need to override to simply start and finish activity without destroying the main screen?
Edit--
I don't have FLAG_ACTIVITY_CLEAR_TOP attached to any of the activity

Using EditText in Activity Group

In my application i am using tabhost. And to control different activities i am using activity group. I have Activity A from which i am going to activity B. Activity B contains Edit Text, Spinner, Button etc. Now when i scroll the Activity B and press the device Back Button than it does not go to the previous Activity. It goes out of application.
Kindly suggest the answer.
This is the way the Activity lifecycle works for Activity Group. Try using fragments instead of activity group with your tabhost to get your movements. Also, please edit your question title to more accurately reflect the actual question.

Categories