i want to switch between activities that use WebView layout.
Here is the scenario
First, i call a WebView activity (say, SecondActivity) from first activity, with an intent containing URL string. Using that string, the so-called activity load the webpage until done.
I then switch back to the first activity by calling startActivity(firstActivity) inside the
Afterwards, when i try to switch back to the SecondActivity, the webpage is reloaded. It seems that the state of the SecondActivity is not saved and method loadUrl(url) is called again.
To switch back to the first activity you could just call finish() in the second activity?
The state of an activity when switching from one to another is not possible to maintain. This is because of how the Activity lifecycle works, when an Activity is returned to from another, the onCreate is called again reloading all your views, or whatever code you have in your onCreate, which is probably configuring all your views.
Related
I have a problem. According to the drawing, I have an activity that has three fragments, each fragment has its recycleview with the items to choose from, so far so good.
My goal: when I click on an item, it opens another activity, legending it with a variable, so far so good.
When I press the back button, I leave the second activity and go back to the first with the variable, the problem is when I return to the first activity, I tried to trigger a function that analyzes the variable within onResume (), but this function it doesn't work, I repeat, it doesn't work with fragments. What return function works on fragments? Is there another simple alternative?
See the image here
Start your child Activity for result using startActivityForResult() and then in your child Activity set the result, after you have collected your data, using setResult() and manually finish your child Activity using finish(). Then in the Fragment class again, override onActivityResult() to receive the data set in your child Activity.
Refer to this guide for more in depth information: https://developer.android.com/training/basics/intents/result.
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.
I am having multiple activity in my android app as well as multiple fragment for each activity. From a fragment of third activity, I want to close the third activity on a back press. I have tried using this inside onDetach() function.
getActivity().finish();
This closes the current activity but while opening the same activity again, the app crashes.
finish() is the final call before the activity gets garbage collected.
You should either not call this at all since there's no real reason to do so. If you do want to return to it later, you can't. You can only create a new Activity. As for fragments, anything that happens on the parent happens on the fragment too.
If you want to close a specific activity, you have to keep a reference to it somewhere where the fragment can access it. Preferably wrapped in a WeakReference so it won't leak memory.
I have a number of different webviews, all in their own activity.
When a menu item is pressed a new activity loads and so does it's webview.
Now clicking between activities reloads each webview, is there a way so that when changing activities the web page doesn't reload and it remembers where the user was?
Thanks
You can use WebView method saveState()/restoreState() and save it to activity onRestore bundle and use saved value in onCreate method if the previous Activity will be destroyed.
You can use similar method which is described here (but used for only configuration change):
http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/
I have an Activity A with a button on it which when clicked navigates to Activity B. Now from this Activity B when the back button is pressed i get back to Activity A which is quite obvious. But once again when i click on the button on Activity A a new instance of Activity B is launched.
My query: Is there a way so that when i press the back button on Activity B it's instance is stored and so when i again click the button on Activity A the saved instance of B is launched instead of a new one.
Hoping for a solution..
if you don't want to pass data.. or just want to go back on Previous activity
finish() in onClick
http://developer.android.com/reference/android/app/Activity.html
In addition, the method onSaveInstanceState(Bundle) is called before placing the activity in such a background state, allowing you to save away any dynamic instance state in your activity into the given Bundle, to be later received in onCreate(Bundle) if the activity needs to be re-created.
To think that it won't be easy to deal with it. The activity in the android can be store but you cannot call the android system to using that instance of your when you navigate to the activity B.
Read this link http://developer.android.com/reference/android/app/Activity.html
it give all information that you can do with it but not all as you want.
When you navigate from activity B to activity A the android itself has been store your activity B's instance temporarily in the memory and that instance will destroy completely anytime later. If you navigate it from A to B again, then the instance of Activity B will be reuse again if it still exist in the memory otherwise it recreate the activity again. In any case the activity B is reuse or recreate, the method onCreate is always call (all method in Lifecycle like onResume ...etc) then all data of your activity B is now new data that's not your old data before you navigate it to the activity A. This is what android system doing so that you cannot launch activity B without calling those method.
if you want the activity B look like the same as before navigate to activity A you should probably saving the necessary data in the method onPause() on the activity B then set those data back to the component of activity B when it call onCreate(). The method onSaveInstanceState(Bundle) should not be use in this case because the android os version which is before honeycomb (3.0) is not invoked that method.
This could be difficult to deal if you are working with online data or listview, webview ...etc and also save data by your own it could the risk to your application itself.
Anyway I hope this could be some help to you.