I have a project that has A ListView and some Buttons, and some ArrayList and String.
I fill out the ListView from a DB Query, that takes some seconds to Load, and when its loads,end-user do something and go to next Activity Via StartActivity(myIntent) method.
When goes to next activity, it will be back to this activity and cause it has a DB Query, takes some seconds and even if network got problem, it get force close Message.
How can i save the whole instance of this Activity just once time and again Restore it in second time?
Im new to android, any help will appreciate.
This is my onCreate Method :
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.type_of_dairy);
// all methods, views, initials and ....
}
In case your use-case is that you need to start the first activity from the second activity using startActivity method. You can do the following to bring the existing first activity to front:
Intent intent = new Intent(this, FirstACtivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
//intent. // Your code here
startActivity(intent);
Basically this flag "FLAG_ACTIVITY_REORDER_TO_FRONT" will bring the existing activity to top. So, your onCreate() method will not be called again and your issue will be resolved. In case you want to execute some code when this happens, you can use onNewIntent() method of the activity to handle the new intent and initialize your variables/fields there. Let me know if you have any doubts.
Related
I had created an android app which has 3 activity (A,B,C) in activity A there is 2 autocomplete textview which fetch data from database and button after selecting data in autocomplete text user press button and it goes to next activity which show listview.
Now the problem is when I press the devices back button from listview is display activity A with selected data in autotext how can I avoid the selected data when I press device back button.
Thanks for helping in advance
When you press the back button, activity A will "resume". So, in your Activity A override "onResume"...in the onResume method, clear your fields.
There is nothing with the listactivity's back button as you are trying to clear you field in the previous activity. As you are not finishing your previous activity while creating the list activity so the previous activity is not changing. So while you are returing from the list you are saying the same data as before. to do so as #Dave suggested you could have done the clearing in the oResume method or onPause method. But there is a problem. If you clear your data in onResume or in onPause you data will be cleared for other pausing or resuming reasons like pressing home, or for other applications etc. So you can do any of two
Option 1:
clear the data of the previous activity when you are starting the list activity
or
Option 2:
instead of startActivity call startActiviyForResult and also override onActiviyResult method. So there you can detect when you are returning from list activity. Then clear the data.
To deal with this problem, I would simply override onBackPressed() method, and in there, I would have cleared activity stack as there is no logical explanation to keep activities in memory, and then start a fresh intent to your original activity.
Here is code.
#Override
public void onBackPressed() {
Intent intent = new Intent(this, Activity_A.class); //I suppose they are in same package
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
I hope it helps.
protected void onResume(){
super.onResume();
setContentView(R.layout.activity_main);
}
this also works well..
you should implement startActivityforResut
When you come back to this activity on result you can clear the data
In my main activity I want to override the onResume method so it acts differently, depending on the activity, which was opened before.
For example: if the user creates a new Item for the listView (in the addNewItem activity), I need to reload the database to display the newItem.
But if he just switches into an activity, which doesn't change anything with the objects displayed in the main activity, the database shouldn't be reloaded and the GUI shouldn't be build again.
Therefor I need to know which activity was 'opened' before.
I hope you understand my problem.
A dirty way is to extend your application class and set an attribute lastActivity that you would set in every onResume/OnPause methods of every activities in your app. This way, when your main activity on resume is called, just read this lastActivity field to know where you come from. But I think it's the dirty way to do it :D
You can send something through in your intent when you call the Activity.
Intent myIntent = new Intent(Activity1.this,Activity2.class);
Bundle myData = new Bundle();
myData.putString("previousActivity", "Activity1");
myIntent.putExtras(myData);
startActivity(myIntent);
Then in the new Activity, you access this and compare the result:
Intent myLocalIntent = getIntent();
Bundle myBundle = myLocalIntent.getExtras();
String str1 = myBundle.getString("previousActivity");
if string.equals("Activity1"){
// do code changes for activity 1 here
}
There are slightly more refined ways of doing this (i.e passing through an int variable which corresponds to a particular Activity) but this is the most basic way.
The right way to do it, is to start every activity with startActivityForResult(intent, resultCode).
When an activity exits, it can call setResult(int) to return data back to its parent.
Link to Developers Resource
I have an activity that is started from another activity via a button and which gets passed an intent when started.
I now figured out, that the activity is then created several times, since it is started with an intent, and will call onCreate() every time I hit the button mentioned above. In the documentation I read it says "Every time there's a new intent for a "standard" activity, a new instance of the class is created to respond to that intent", so that I assume that I then have several instances of that activity somewhere on the stack. This is what I would like to avoid, since the activity has some static variables which are referenced from other activites and I want to make sure that the value of this variable is deterministic!
What I want is to get rid of all existing instances and create a new instance of the activity when I press the button, or make sure to have just one activity and create it with the new intent everytime.
I tried to achieve this by setting android:launchMode="singleTop" for the activity and implementing onNewIntent(intent). This should make sure that I only have one instance of the activity in this scenario and I get the new intent. However if I do it like this, I basically have to copy my whole onCreate() method, but just using the new intent, which feels wrong.
public void onNewIntent(Intent intent) {
myAdapter= null;
serverThread.stopThread();
serverThread = null;
this.onCreate(...); // <- somehow do all this with new intent
}
Do you have a good idea how to solve this situation easily?
You can move all that code from onCreate() to onStart() to make sure it's called every time that activity is being opened
Try setting the flag FLAG_ACTIVITY_REORDER_TO_FRONT
It will ensure that if an activity exists in the stack, it will bring that activity to the front.
Intent intent = new Intent(getApplicationContext(),
YourCustomActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
In the above situation actually onDestroy() is called when the user is navigating back to the previous activity with the button creating the intent and starting the activity. I.e. actually in this case there is already only one instance of the activity and the previous one is being destroyed.
For this reason onNewIntent() is not called in the above setup. If you want to e.g. stop a server, as indicated above in the question by a piece of code, so that you can start a new one when pressing the button again, you hence can actually simply do this:
#Override
public void onDestroy() {
serverThread.stopThread();
super.onDestroy();
}
Hope that helps others :)
I call an activity called Activity1 from an Activity called MainActivity using the following:
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
So, when the control comes to Activity1. The normal activity lifecycle is started. i.e onCreate() is called and so on.
when i click the back button when the control is in Activity1. The finish method is called, which in turn calls the onDestroy() and now the control is back with the MainActivity screen.
#Override
public void onBackPressed() {
Log.d(TAG, "onBackPressed()");
finish();
}
The next time i call Activity1. The onCreate is called again as i called the onDestroy (when i pressed the back button) from the previous call.
Question:
1. is there a way to pass control back to the MainActivity when the back button is pressed without having to call the "finish()" method?
2. problem with calling finish, every time i call Activity1 from MainActivity, A new instance of Activity1 is created. that is the lifecycle again starts from onCreate()..
i do not want this to happen as this is has become a major performance issue.
The main point i'm looking for is whether i can start the activity1 from the resume state rather than oncreate, when i call it after the first time.
I don't believe you need to call the "finish()" method on onBackPressed. Android does that for you when you press the back button. The onBackPressed is used to last minuet tidy up (save stuff to sharepreferences, etc).
Android default behaviour is to call onCreate whenever a new activity is place on the screen. You cannot call a new Intent without this to happen.
I'm not sure why this is performance issue for you. Can you go in a little more detail what activity1 is doing? Are you doing heavy network communication? Is it possible you can cache the store results?
in Actitity1 you define your WebView as:
private static WebView webView = null;
in onCreate() you only create it if it's null:
if(webView == null){
//create webview and load from network
}
Use this approach wisely as it may easly lead to memory leaks if you point to objects in other activities, or objects that may be kept alive (runnables, messages, etc.)
I want to start a Activity A from status bar notification, When the activity A is already in front then i want to finish that and fresh start activity A. How can i do this?
Review the documentation on creating Status Bar Notifications. This definitely covers starting and Activity from a Notification using an Intent and PendingIntent.
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
As for if the Activity is already running, finish it and start it freshly... I'm not sure that can be done easily, depending on what you really want. You may be able to do something with the launch mode activity parameter in the manifest:
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
And then have your activity respond (with onNewIntent() most likely) and "reset" itself programmatically. Possibly with something like this:
Android restart my activity
You mean re-start Activity A? While the most common approach is just to re-launch a new Intent with your same class I think it uses way too memory. I'd rather create an "init" method which should be called from the onCreate AND when you want to re-launch your activity. Example:
public void onCreate(Bundle si){
// Call super and set your layout...
init();
}
/**
* This method should be called whenever you want to restart your activity. The
* biggest advantage is you already have your layout (setContentView() method)
*/
private void relaunchActivityA(){
// Clean or save anything you need to clean or save
init();
}
private void init(){
// Init your variables, threads, and so on
}
If you wrote 'finish that and fresh start Activity A' instead of 'Activity B', then right after your startActivity() -on activity A- call 'finish'. Example:
// This is inside Activity A
Intent i = new Intent(this, ActivityB.class);
startActivity();
finish(); // This will be called right after 'Activity B' finishes