Back button in an Activity without recreate previous Activity (Android) - android

I have a master activity that is a menu (A). The activity A have 6 fragments. One of them is a gallery of pictures (Figure 1). When you touch a picture you go to the picture details activity (B) (Figure 2). I add the tag "parentName" to the activity B in the AndroidManifest.xml. So the parent of the activity B is the activity A. The problem is: when you are in activity B and you press the Up Button (Figure 2) the activity A is recreated and it shows the first fragment of the activity A (not the gallery fragment). I want the same behavior of the Instragram app when you back to a previous activity. It seems like the Instagram app don't recreate the previous activity. The expected behavior is also similar to the behavior of the back button (Figure 3). How can I achieve this behavior? Thanks
Figure 1. Picture details Activity
Figure 2. Picture details Activity
Figure 3. Back button

I've solved my problem easily. I just had to add the tag android:launchMode="singleTop" to the activity A. I also add the next code in the activity A but probably isn't necessary. I hope you find it useful.
#Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
}

May be programatically you are using this :
getFragmentManager().beginTransaction().replace(R.id.content_frame, new FragmentB(),null).addToBackStack(null).commit();
Replace it with this line :
getFragmentManager().beginTransaction().add(R.id.content_frame, fragment,null).addToBackStack(null).commit();

Related

How to come back to my parent-activity without losing data?

I have an activity A with a "child activity" which is the activity B.
When I create an activity, like in this case in android, I choose for activity B, that his parent shall be activity A.
So, when I start my application, I have an icon in the appbar in the activity B, where I can go to the activity A.
In my case, the user for example edit-text and manipulate some variables in the activity A and call activity B with Intent.
NOW, in activity B, when I click on the "back" icon, I will go back to the parent activity in this case activity A and all manipulated data are "away", because the screen will be relaunched.
But when I dont click on the icon in the activity B but instead click "back" on my mobile phone, I will come back to activity A and all manipulated data will be still there.
SO my question is, is there a way, to come back to activity A by clicking the back icon in activity B, without that the screen will be relaunched. Because this icon was automatically added, when I say, which activity have to be the parent class. I would like, that I can go back with this icon, but not relaunch the screen, instead continue, where I was "last" time.
I show you a part of my manifest file for the activity B:
<activity
android:name=".strassenfuehrer_screen.VideoPlayerHandlungsleitfadenStrassenfuehrerActivity"
android:label="Straßenführer > Handlungsleitfaden > VideoPlayer"
android:parentActivityName=".strassenfuehrer_screen.MitHandlungsleitfadenStrassenfuehrerScreenActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="de.derdoenerdon.ressourcencockpit.strassenfuehrer_screen.MitHandlungsleitfadenStrassenfuehrerScreenActivity" />
</activity>
Here you can see the "back icon" in the activity B.
For help, I would be very thankfull, I am new in android and have difficulties with it.
Thanks a lot
In your manifest for Activity A add this attribute:
android:launchMode="singleTask"
This should solve your problem.
This makes sure that there is only one instance of Activity A in the BackStack. Therefore Activity A is only created once per Task.
Hope this helps.
for more information https://developer.android.com/guide/components/activities/tasks-and-back-stack
You can override onSaveInstanceState() into your Activity and save your data before leaving that Activity.
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt("KEY1", Value1);
savedInstanceState.putInt("KEY2", Value2);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
On coming back to that Activity you can get the saved data as bundle into you onCreate() method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Always call the superclass first
if (savedInstanceState != null) {
// Restore value of members from saved state
int val1 = savedInstanceState.getInt("Key1");
int val2 = savedInstanceState.getInt("Key2");
} else {
// This is the case when you are openning this Activity for the for the first time
}
}

Activity backstack handling

Suppose I have activities A, B, C, D, E.
when I open application with activity A and switch to activity B and further to activity C. Suppose, somehow B gets destroyed by system. and when I press back button, I came out of app. What I really wanted was to be able to go to B if it exists otherwise A (landing page). In normal cases it's working smoothly but it's some cases where I have to interact with other apps/interfaces for eg. open url in browser. After sometime if I go back to app, I see latest page as it is but when I press back button app exits.
I have searched for solutions but couldn't find one so posting it as a question.
I know the idea of passing extra in intent and starting new intent on back press but these would not work in some cases or requires creating a backstack handler of own.
There is no guarantee that your activities will stay in memory and this is an intentional behaviour.
Your option is use to onRestoreInstanceState and onSaveInstanceState so that user do not lose critical data.
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// Get data
super.onRestoreInstanceState(savedInstanceState);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
// Save data
super.onSaveInstanceState(outState);
}
One way which I have used in the past to validate activity state is to add e.g. a boolean attribute in your activity, set this true when activity starts, if your activity is destroyed after some time this will be false. Check state by overriding the activities onBackPressed method?
e.g.
onBackPressed(){
if status{
/*activity live*/
}
else{
/*activity destroyed/*
}
}
This might work If I understand you issue correctly.

Android back pressed without refresh activity

I have 2 activities which one activity leads to the other activity.
The first activity present a listview and the items click leads to the second activity.
When I click the back button I get back to the first activity but the list reload and scroll up to the first item. I want the list to stay at its place after I get back to it.
If you call finish() in activity A when openig the new Activity B on back press you will call onCreate() of activity A to avoid this avoid calling finish() in activity A in your onItemClickListner()of activity A and record tge position of tge click in Activity A, in which case calling the back press in Activity B will cll for tge onResume() in activity A where in you could call For a direct scroll:
getListView().setSelection(<position>);
Or For a smooth scroll:
getListView().smoothScrollToPosition(<position>);
when you press back you call oncreate() on that activity so everything reload
you could use :
getListView().smoothScrollToPosition(yourpostion);
and think about using fragments for another way around it .
and recyclerview is advised .
If you don't call finish() on Activity A, even if you go to Activity B and come back, Activity A should not call the whole onCreate() again.
If you take a look at the life cycle of Activities, it will put Activity A in onPause() and probably onStop() depending on what you are doing on Activity B and how you defined launchMode in AndroidManifest.xml.
So when you are calling startActivity(..), don't call finish(). Otherwise it will load everything again to draw the Activity A.
Another possible way is using
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
..
}
OR you can use SharedPreference.
Once fetching data from Parse.com is over (like onPostExecute() of AsyncTask), you can read the data passed from Activity B to relocate the user to the list where they were.
EDIT:
Read this article about how to "come back" to the activity you were in, too.

Activity recreate itself when press back button

I have 2 activities.
ActivityA : i use ImagePickerActivity from poly-picker library to select multi files and send paths to ActivityB
ActivityB : each file, i create one view and add to relative layout programmatically.
But my problem is when i press back button, ActivityB not finish, it recreate itself. number of recreate times is equal to number of files.
Pls, help, thanks and sorry about English.
When you press the back button, the Activity gets destroyed. So, when the activity is started again, it recreates itself.
Override onBackPressed() in ActivityB
public void onBackPressed() {
ActivityB.this.finish();
}

Android: onSaveInstanceState in Back button

I am developing an application in which i am overriding the back button. I created a check box. On click of which i am calling intent for:
startActivityforResult();
And also maintaining the state of activity as :
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putBoolean("checkbox", checkbox.isChecked());
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
checkbox_state = savedInstanceState.getBoolean("checkbox");
}
which run fine and the state is maintained.
Means i am entering value in edit text. and on check box click calling new activity for result and while return on first activity the state is maintain.
Now but from second activity if i click on device back button the state is not maintained.
So what should i do to maintain the state on back button. I searched but not found satisfied solution. Please suggest me.
When You start the new Activity then the current Activity gets hidden and new Activity is placed on top of the stack. Now if you press the back button on the new Activity then the first activity should come up in its current state (If it wasn't destroyed, calling finish()) where it was left i.e. if the check box was checked then it should remain checked. You don't need to save the activity state unless the orientation is changed or the activity is destroyed.
Are you sure you are not doing any thing in the onActivityResult or onResume() method which effects the state of the check box? I would recommend to first comment out all the code in both the methods and see if your check box retains the state. Also can you also make sure that the code itself doesn't uncheck the checkbox before starting the new Activity?
Now but from second activity if i click on device back button the state is not maintained.
onSaveInstanceState() is mostly used for configuration changes (e.g., rotating the screen).
So what should i do to maintain the state on back button
Most likely, there is no "state" that needs to be "maintained", outside of your data model. Your activity needs to update its data model: files, database, preferences, ContentProvider, some singleton that is your in-memory data model manager, whatever.

Categories

Resources