I have 2 activities. Inside the manifest activity 1 is set as Launcher and activity 2 is set as Home. Now i want to call the second activity from the first one to trigger the window for setting the home launcher and then the app should be selectable.
I have tried the following:
Intent selector = new Intent(this, homeActivity.class);
selector.addCategory(Intent.CATEGORY_HOME);
selector.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(selector);
But it seems not working.
Is this somehow possible? Do i need to change the Default activity?
Try this :
selector.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
Related
When my activity stack is like,
MainActivtiy -> Activity1 -> Activity2 -> Activity3
I need to go 3 back states to reach MainActivtiy from Activity3
I could be able to close opened activities from Activity3 like,
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
But, above code segment closes all activities including MainActivtiy and starts new MainActivtiy. I want to know are there any other options to do the task that I need. I dont want to create new Intent because it drops the data of static methods that I have created in my MainActivtiy.
Please help me, Thanks in advance.
You should use FLAG_ACTIVITY_CLEAR_TOP which will bring the running activity on top and remove all other activities above it
Note : it will trigger the onNewIntent of already running activity otherwise mention launchMode = "singleTask" in activity tag, inside manifest
I'm using this library which is a Wizard.
When user is done with the Wizard and clicks Done button he moves to another Activity.
I have added to Manifest file android:noHistory="true" for the WizardActivity and also when I start the next activity im doing it like this :
Intent intent = new Intent(this, ProjectsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Nevertheless when user click backs he still goes to the WizardActivity. I'd like the user either to exit or go the activity that started the WizardActivity.
Thank you
finish() the activity after launching the other Activity. i.e after startActivity(intent) add finish()
I want to always start launcher activity after app goes to the background. I tried android:clearTaskOnLaunch="true" but it not work. How can i do this? For example i have A,B,C,D,E,F activity. A is launcher activity.
now i open app so A activity is called then A>B>C. now i minimize the application. Now i open application from the icon so app is start from C but i want it start from A so how can i do this? some said use onResume intent with Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK. but what if i press back button then onResume is called and my app start with A activity.... so it is not work..any solutions???
You can check the App is in foreground or is in background and when the application goes to the background state at the same time do some logic that when your application comes to foreground it will always show the root activity.
How you can identify application is in which state is well explained here
When you start Activity B, use these flags. That way Activity B and C will always be in a separate stack. That means that when someone starts Activity A from the home screen they will see Activity A always.
Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK
Intent intent = new Intent(context, B.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)
startActivity(intent);
Also this might help, you can add this flag to your root Activity.
<activity
android:name=".ui.MainActivity"
android:clearTaskOnLaunch="true"/>
This does because device store data about application. For short time data are stored. if you want to start application from rot class. use service with application, so that whenever your application goes in background and pause then choose your action to do that will be easier.
I have a spinner that essentially starts multiple new activities based on onItemSelect();
However, what's happening on my app is if I start the same activity multiple times I have to hit the Android back button multiple times. How do I start the same activity and kill the previous one so that I don't have multiple layouts sitting there open?
Set android:noHistory=true for your <activity> in Manifest.xml. See here
Or, programatically:
Intent intent = new Intent(this, Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Use the FLAG_ACTIVITY_CLEAR_TOP activity flag.
When you start a new activity you need to finish your old activity. So when you call your new intent:
startActivity(intent);
finish();
That will end the current activity.
You might want to consider setting the activity launchmode to "singletop" (this can be done in the Android Manifest). Then instead of creating a new activity, it will call OnNewIntent() of the existing activity.
I'm trying to implement a good Up navigation on my Android app.
For instance I want to go from DetailsActivity to MasterActivity. To do so I use the following code:
final Intent upIntent = new Intent(this, MasterActivity.class);
upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
supportNavigateUpTo(upIntent);
It works great if MasterActivity is already open in the background: it closes the current activity (and all the activities in the stack over MasterActivity) and fires the method onNewIntent on MasterActivity.
But, if MasterActivity is not in the stack, the method simply closes DetailsActivity and nothing else happens.
What I would like to achieve is that, if MasterActivity is not in the stack, the current aActivity gets closed and a new instance of MasterActivity gets created.
I already tried, with no success, the following flags combination:
Intent.FLAG_ACTIVITY_SINGLE_TOP
Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP
Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK
Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK
Have you any idea on how to solve my issue?
Thanks a lot for your time
EDIT
The parent Activity need to be specified at runtime. In fact, the parent Activity can be ActivityA,ActivityB or ActivityC.
Let me better explain the problem. My app manage TV Shows. I have a MainActivty, a TVShowActivity, a SeasonActivity and a EpisodeActivity.
In the navigation drawer from every Activity I want to be able to jump to the parent Activity:
-On EpisodeActivity I want to go to SeasonActivity, TVShowActivity or MainActivity;
-On SeasonActivity I want to go TVShowActivity or MainActivity;
-On TVShowActivity I want to go to MainActivity.
My app currently works if my Activity stack is something like: MainActivty => TVShowActivity => SeasonActivity => EpisodeActivity.
The problem is that sometimes the user can reach EpisodeActivity directly from MainActivity, and I want to be able to use the same navigation pattern.
On each activity you can keep for example two buttons providing an onclick event in the properties of their xml and provide intent code in java class:
` public void youronclickname(View v)
{
Intent myintent = new Intent(your sourceclass, to destination class);
startActivity(myintent);
} '
similarly try this with your next class