Activity started with request code does not launch with singleTop - android

I'm trying to apply single top to an activity that starts with a request code. I have tried both applying launchMode=singleTop to the activity in the Android Manifest, as well as setting the Intent flag for singleTop. However, if I launch the activity with a request code other than the default (-1), it will never call onNewIntent and will instantiate several activities on top of each other on the stack. Once I remove the request code and have it default to -1, however, it respects the launchMode I define.
I carefully read over the documentation, but was unable to find anything there or elsewhere online that might explain why android seems to ignore launch mode when an activity is started with a request code. Is there a work around for this? Why does it do this in the first place?

Related

Destruction of views on returning to activity

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."

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.

I need to use "singleInstance" in main activity, and also i need bring another activity to the top

I need only one instance of my app, so I use android:launchMode="singleInstance" in main activity section in manifest, but this causes: when I click on home button when second activity is displayed and launch my app again, main activity is displayed, but I need to display second activity (I need standard behaviour). Problem is caused by using singleInstance in activity's manifest so this activity is always on top. I tried to launch second activity with various flags, but this doesn't work. (android:launchMode="singleTask" doesn't work too).
singleInstance and singleTask are only needed in very rare cases.
You probably don't need any special launch mode to get the behaviour you want. Just try the standard launch mode and see what happens. In most cases you don't need any special launch mode unless your application is started by other applications and you want to be able to control that.
If you try the standard launch mode and have problems, please indicate what the problem is in more detail and we can probably help you.

Restart Android Process While Returning Activity Result?

I want the ability to start my activity in a new process and end the current instance of my activity in its current process.
The second part is easy enough with:
android.os.Process.killProcess(android.os.Process.myPid());
The first part I can not find a documented way to do. I can however get the same effect if I do the following:
((Activity)context).setResult(reason);
((Activity)context).finish();
android.os.Process.killProcess(android.os.Process.myPid());
where the activity was started for a result. This seems to work every time. The only side effect is when my activity has no title there is a brief flicker as the new instance removes its title. The activity restarts and the onActivityResult is called with the parameters I had set before killing the last instance of the activity.
Even though this works every time for me now my concern is that this will not work under some circumstances I have not tested or may be considered a bug that is removed from future OS versions.
Is this an expected and correct behaviour?

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.

Categories

Resources