How can I make an activity on be openable when launched? - android

I have an app where my LAUNCHER activity, named dispatch activity, which sees whether the user has logged in before or not(in onCreate()), and then launches the correct activity. And when the correct activity is launched, I can tap the back button on my device and it goes back to the dispatch activity, and it stays there until the user refreshes the app. I only want the activity open-able by LAUNCHER. So if the user clicks the back button on the device, it stays there, like the dispatch activity doesn't exist. (By the back button I mean by that icon next to the home button on the tablet)
Hope you can help

Have you tried this : when you start the second activity, at the same time you exit (run finish()) on your dispatch activity?
EDIT: like this: (in your Dispatch Activity)
//quit your Dispatch Activity
finish();
//start new activity
startActivity(new Intent(this, YourNewActivity.class));

I'd recommend to play with intent flags. In this particular case, have a look at
FLAG_ACTIVITY_NO_HISTORY.
Set it for your dispatch activity right in the manifest and it won't be kept in the history stack.

Add LAUNCHER activity as a FLAG_ACTIVITY_NO_HISTORY.
Just define in your manifest file:
<activity android:name="LAUNCHER ACTIVITY"
android:noHistory="true"
...
></activity>
Now validate your user login stuff and just start your next activity. Rest of the stuff will be taken care by Android.

Related

Stopping multiple activities from showing up

I have it so when you click the notification icon on my app it opens up an activity. The thing is that if you click it over and over it opens the same activity over and over. So when you click the back button you have to go through all of those before going back to the beginning. How could I make it so that if that activity is already there it doesn't reopen it?
Check out the launchMode section of the manifest documentation.
You're looking to add:
android:launchMode="singleTop"
to the declaration for your Activity in the manifest. This means that if an instance of the activity is already at the top of the target task, the system will re-use that. Any intent gets delivered to the onNewIntent method of that activity.
I'd recommend having a good read of all the options on that page and deciding which one is right for your usage.
Try putting this in your manifest:
android:launchMode="singleTop"

how to know if Activity resumed by shortcut or by another top activity

The main activity creates a shortcut to homescreen, all goes good but it needs to know an extra value when it starts by shortcut. So I used this while creating shortcut
intent.putExtra(EXTRA_ID, "Some string info here");
And onResume retrieve value like this
if (getIntent().hasExtra(EXTRA_ID))
ShowToast(getIntent().getExtras().getString(EXTRA_ID));
Everything works fine, lets take some examples
Activity never launched, Home screen shortcut pressed and A toast appears, Nice.
Activity is in background, Home screen shortcut pressed and A toast appears, Nice.
Activity is in use, another activity is launched, when back, toast appears, Not nice.
Activity is in background with no extra, Shortcut pressed, Nothing appears.
In 3rd example, it does what it suppose to do. But how do I know if activity is resumed/launched by shortcut ?
You set an extra in launcher Intent, and want to get the extra when this activity is opened in any state.
The problem you meet:
Activity is in background with no extra, Shortcut pressed, Nothing
appears.
This is because that your activity is started already, the launcher intent just use the existing task as the front task, and its extra data is lost in such situation.
To solve this, you need 2 steps:
Add android:launchMode="singleTask" to this activity in your manifest.
Put the extra data handling codes in both onCreate() and onNewIntent(), not in onResume().
How are you removing the extra EXTRA_ID? You should do it like:
In onResume():
if (getIntent().hasExtra(EXTRA_ID)) {
ShowToast(getIntent().getExtras().getString(EXTRA_ID));
getIntent().removeExtra(EXTRA_ID);
}

How to start android activity without showing main activity in the back

I'm developing an application containing 2 activities:
Main activity launched where user starts the app.
Second activity is launched where user clicks on a button widget
on the home screen.
I created my widget and the view that will be displayed when the user clicks on the widget button.
My problem: The second activity UI is displayed above the main activity, the main activity is visible because the second activity layout is transparent, because I want to let the home screen still be visible when clicking in the widget.
How can I launch only the second activity without launching the main activity below it? I tried to remove <action android:name="android.intent.action.MAIN" /> in the main activity from the manifest file, widget works but when I install my app in the phone there's no app icon installed so I can't launch the main activity.
I hope that I have expressed my problem.
You have to finish the first Activity right after you start the second Activity
startActivity(<secondActivity>);
finish();
You can either finish your main activity after you start your second activity:
startActivity(secondActivity.class);
finish();
Or in your application manifest you can use the noHistory flag on your main activity, so it calls finish itself when you navigate away from it.
android:noHistory="true"
Perhaps having a different intent action and intent filter associated with the second activity will work. Have a look at the documentation. The notepad application example is should be similar to what want need to do.
I solved my problem like this :
1- I added to the main activity attributes in the manifest file :
android:launchMode="singleTask"
I tested it does not makes loading time when re-launching activity longer than usually because this attribute does not erase the app cache and keeps the intent instance in the memory.
2- Add this to main activity class :
#Override
void onPause()
{
super.onPause();
finish();
}
Thank you all for your help.

Prevent Background Activity From Resuming

I have a app that sets the main Actvity as the main launcher. How can I prevent my current foreground Activity from closing whenever I press the Home Button which calls the background activity from the bottom of the stack. Is there a way to trap the intent and allow the the foreground ativitiy to finish. My manifest has the following:
android:launchMode="singleTask"
There is no way within the public APIs to "override" the home button in the manner that you wish.

block back button for ceratin activities

I have two activities A and B. What I want is to show activity A as an intro, the user will be unable to navigate back to it. Is there some flag I can set to activity A to do this? Can I block the back button for one activity only? Activity A is of course my main activity which automatically starts activity B after some "hard work".
Best regards.
you do not need to block the back button, but just call finish() on your A activity after firing an intent to start B. Back button pops the previous activity from activity stack and it won't be able to pop A if it is already finished.
For this you don't need to block the Back button. Simply, start the second Activity and quit the first one. And now if user presses the Back, they will be taken to the Android home screen not on your apps home screen.
Updates: By the way if you want to intercept the Back button for any reason, simply override the onBackPressed() method of Activity class. See this for details.
Never override the functionality of a hardware button.
You should call finish() in Activity A right after starting Activity B (calling the Intent).
it works but the application terminates and i'm redirected to android's applications screen. I would like to stay in activity B if back button is pressed, i don't want to exit the app. here's is what i got :
public void startProgram(Context context){
Intent intent = new Intent(context, ActivityB.class);
startActivity(intent);
finish();
}

Categories

Resources