Destruction of views on returning to activity - android

I have a MainActivity class, which launches a RaceActivity at some point. If, later, the RaceActivity exits, I want the MainActivity to return, with all its views. However, it seems to be created anew each time. I have implemented the proper section of onOptionsItemSelected, but when I click the back button, I get a new instance each time, or at least the programmatically added views are gone. What can I do to fix this?
Edit for clarification:
I am fine using onCreate with a bundle to restore these views, but I thought that happened automatically if you recreate the same instance of an object. I want to keep programmatically created views when the activity is recreated. Alternatively, I want the activity to stop being destroyed when the user returns to it. (I tested, and it gets destroyed as soon as the use returns.)

Accept my answer if it helps you. it take hours to find the solution..
override onBackPressed()
Intent intent = new Intent(FromAnyActivity.this, CurrentActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
and insert the code in it.. It will reorder your Activity from stack and your views and data will remain where it was. There was another way by adding SingleInstance launch mode.. read these link may help..

Add the following to your MainActivity declaration in the AndroidManifest file:
android:launchMode="singleTask"
According to the Android APIs, this addition is for: "The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one."
If the above doesn't work, I believe you can also try adding this as well to the MainActivity in your AndroidManifest file:
android:alwaysRetainTaskState="true"
According to the Android APIs, this addition is for: "Whether or not the state of the task that the activity is in will always be maintained by the system — "true" if it will be, and "false" if the system is allowed to reset the task to its initial state in certain situations. The default value is "false". This attribute is meaningful only for the root activity of a task; it's ignored for all other activities."

Related

Android intent reception and rotation

I'm using an Intent Filter in my activity to retrive an url clicked on by the user.
In my activity onCreate method I have the following code
Intent intent = getIntent();
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
url = intent.getDataString();
showDialog(DIALOG_ID);
}
It works great except when I rotate my phone. Even if tha dialog was closed prior to the rotation, it reopen every time I change the phone orientation.
I can I avoir that.
For your information I don't want to lock the orientation
Another solution that doesn't require handling configuration changes yourself might be to simply check if the savedInstanceState Bundle parameter in onCreate is null before showing the dialog.
If you look at the docs for onCreate you can see that savedInstanceState will be non-null when the activity is recreated (due to configuration changes for example) and thus will be null when the activity is run fresh.
You would typically call setIntent(null) to remove the intent used to summon the activity. However, in practice, it doesn't always work. Apparently, a common workaround is to set the intent's action or data or both to null, depending on what you use in code. In your case, after showing the dialog, I would probably go with intent.setAction(null).
This is a pretty simple fix. In you manifest file, locate activity and add this:
android:configChanges="keyboardHidden|orientation"
This will prevent your logic in onCreate to fire again (or so I believe)
Quoted from here:
In some special cases, you may want to bypass restarting of your
activity based on one or more types of configuration changes. This is
done with the android:configChanges attribute in its manifest. For any
types of configuration changes you say that you handle there, you will
receive a call to your current activity's
onConfigurationChanged(Configuration) method instead of being
restarted. If a configuration change involves any that you do not
handle, however, the activity will still be restarted and
onConfigurationChanged(Configuration) will not be called.
I have never seen this issue until today. I did getIntent().setAction("");, and solved it. No more brain damage :)

android sdk. startActivity() moves prog. to background

I'm simply trying to change between xml files/layouts.
In my main activity I have startActivityForResult(new Intent(this,playlist.class), 0);
I've added the class to my manifest, created the class where the package is com.site.mainactivity, and superclass is android.app.Activity.
I didn't "extends playlist" in my main activity
My logcat only shows "showStatusIcon on inactive InputConnection", "InputConnection = android.view.imputmethod.BaseInputConnection#40532b90, active client = false"
When I try to start the new activity/switch layout, my home screen shows, but my app is still running in the background. When I go back to it, the app takes me back to the main screen.
What am I doing wrong?
Thanks
This sounds like a problem with how the Activity stack is being maintained. Specifically this is because based on the Manifest properties, an Activity can have different properties that specify how the Activity should be treated by the manifest ie. if it is included in the Activity stack or not and/or Also this could include where the main entrance of the application is, and whether or not an external intent can go to a specific screen in the application.
With manipulation of this it is easy to control. Look up the ActivityManager and how tasks are retrieved and maintained and analyze the design flow of your application. You must fully understand how you want it to work to fully solve your problem. A flow chart would aid you in this diagnosis.
Learn to control your flow properly.
If you just want to change the layout, you can either:
call setContentView again
use singleTop and launch the same activity but with a different layout
close the first Activity with finish() after launching the second
What I did to bypass this hurdle was simply use getLayoutInflater().inflate(R.layout.main, null); as a View and then setContentView(v)
I just need to reinitialize events, views, etc so that the program runs as it did before it changed the view.

android: unusual way to start activity

In one activity I need to start another one but with one condition: if it was started before then it must be finished and started again. This activity show some information about system state (Theme.Dialog style) it also can start some services and so on. As far as I know when I do startActivity(intent) then onResume() will be called (if activity was started before). Does anybody know how to do it?
That's precisely how it should work. If you need to adjust the values on-screen, put that code in onResume(). What may not be obvious from its name is that onResume() is called when the Activity is first created as well. It's always the last method called before an Activity becomes active.
use the NEW_TASK_LAUNCH flag in the startActivity() call. Read documentation http://developer.android.com/guide/appendix/faq/framework.html#4
In the manifest file, in the activity attributes, you have the attribute launch mode, which let you specify how the activity must be launched (http://developer.android.com/guide/topics/manifest/activity-element.html#lmode).
Take a look at the description to see which mode suits most your needs. Then when the activity is brought to front, you can restart your service by overriding the Activity.onResume() method.
In one activity I need to start another one but with one condition: if it was started before then it must be finished and started again
There is nothing that only does this. The closest is to have the combination of FLAG_ACTIVITY_CLEAR_TASK|FLAG_ACTIVITY_NEW_TASK, but that has other side effects, such as wiping out any other activities.
As far as I know when I do startActivity(intent) then onResume() will be called (if activity was started before).
Not by default. By default, a second instance of the activity is created.

Implications of setting a flag on the Intent

I'm calling context.startActivity(intent) from within a seperate OnClickListener class. In order for this to work I had to set the FLAG_ACTIVITY_NEW_TASK on the intent.
That all works as expected, but I'm wondering if there are any implications to doing this that I'm not aware of. Will this create any problems in terms of performance? Does it reflect poor design on my part?
What do you think?
From the Android docs: "This flag is generally used by activities that want to present a "launcher" style behavior: they give the user a list of separate things that can be done, which otherwise run completely independently of the activity launching them."
More from the Android docs: "Note that if this method is being called from outside of an Activity Context, then the Intent must include the FLAG_ACTIVITY_NEW_TASK launch flag. This is because, without being started from an existing Activity, there is no existing task in which to place the new activity and thus it needs to be placed in its own separate task."
As you are starting a new activity each time and adding this to the stack, if you leave that activity and then start another with the onClickListener you might run the risk of starting another activity instead of resuming the previous activity. I think changing the flag to FLAG_ACTIVITY_RESET_TASK_IF_NEEDED should fix this.

Android startactivity and stacks keep in memory

we start activity and do not calling finish() on existing activity it keeps existing activity in stack and move to new activity if we press back button we return to previous activity.
Its mean all previous activities not beeing called finish() keep remain into the memory(Stack)
Now problem is i do not want to call finish for every activity is there any centralize place where i can define that keep only last 3 activities in stack and remove rest of them ?
In the Android Manifest in the [activity] tag you can specify android:noHistory - Whether or not the activity should be removed from the activity stack and finished (its finish() method called) when the user navigates away from it and it's no longer visible on screen —"true" if it should be finished, and "false" if not. The default value is "false".
A value of "true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it. This attribute was introduced in API Level 3.
When launching new Intents that resolve to Activities, consider setting appropriate Intent Flags, list of such flags can be found here. Using these flags, you can re-order activities on the history stack and clear it too. There is an excellent description of this in the Activity and Task guideline documentation see here.
Alternatively, in your deployment descriptor also known as Android Manifest, set appropriate attributes for your activity(noHistory would be a fit for your problem).
Refer this for details.
It sounds like you're worried about memory usage and you shouldn't be: Android handles all this for you.
When one of your Activities covered by another of your Activities so it is no longed visibile to the user it is stopped. A stopped Activity should still retain all its state but can be killed by the system when memory is needed. This is why you need to implement the onStop() and onRestart() methods so that your Activity can recover its state after being killed and restarted automatically by the system.
For more detail you can read about Component Lifecycles in the Androind Fundamentals document in the Android Developer documentation.

Categories

Resources