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.
Related
When my android app is started, main activity is launched. It displays a full screen image for 5 seconds, and then it jumps to another activity using intent. What i want is to kill the main activity, so that when user presses the back button of navigation bar, instead of opening main activity, the app gets closed.
One more thing:- i don't want to keep on destroying previous activities. I just want to kill that one activity(namely main activity), just after the intent is sent to new activity, Because i will be adding more activities.
We can say that my true purpose is destruction of main activity, and making the next activity(out of all other activities) as a activity through which the app can be leaved using back button of navigation bar.
I am not able to properly explain my problem in words, but please try to figure out my problem what what all i have mentioned.
In your MainAcitivity ,call the second activity like this:
Intent intent=new Intent(this,<your second activity.class>;
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
I would add
android:noHistory="true"
to the AndroidManifest.xml, specifically in the MainActivity definition
To kill Activity you have to use method finish();
In MainActivity in code, where you are starting next activity add finish();:
Intent i = new Intent(MainActivity.this, NextActivity.class);
startActivity(i);
finish();
What you're trying to achieve is called a splash screen.
In your main activity start another activity and if the user presses back on another activity, simply call o finish on main activity. Destroying parent activities before child is sort of messy.
I'd recommend googling splash screens through cold app booting.
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.
im looking a way to finish first activity from other
First activity it's my splashscreen. i want to show him while second activity building../downloading datas and interface, and in asynctask from second activity i want to finish first activity.
i dont need simple way with delay.
It's possible ?
Try android:noHistory="true" in your splash screen, you can set this in manifest file.
Like this:
<activity
android:name=".package.SplashScreen"
android:noHistory="true"
...
</activity>
See more here.
I see several options:
Use fragments and load data in activity, when data loaded replace splash with actual data.
Send intent(or event through EventBus) from second activity and catch it with BroadcastReceiver in first activity.
<activity
android:name="package name with class"
android:noHistory="true">
Since you need to start asynctask of second activity from the beginning and meanwhile till the data downloading finishes, I am guessing you want to show the splash screen.
You have two activities, splash screen activity and main activity.
First start your application with main activity instead of splash screen activity.
Inside main activity, start your asynctask and call splash screen activity as child activity using startActivityForResult (Intent intent, int requestCode). Which will open your splash screen activity as child activity. As soon as your background processing finishes, you can close your splash screen activity using finishActivity (int requestCode).
Let me know if it worked out for you.
I have an annoying issue with the activity stack that I haven't found a solution for.
Basically I have an activity that acts as a "starter" activity (the main activity in my manifest, this is started from the launcher etc). It is translucent, set using:
android:theme="#android:style/Theme.Translucent.NoTitleBar"
What it does is check the Intent that is fed to it. If the intent data is empty, it starts a new activity which is the main activity for the app.
If the intent data contains certain commands the starter activity should perform certain tasks and then exit, not even starting the main activity. So this should happen without any UI (except for a popup message when done).
My problem is that if the main activity has been started, if the user uses the home button to leave it, the next time the starter activity is started with a command, the main activity also shows up briefly.
I'm assuming this is because of the activity stack since I'm not restarting the main activity from the starter activity in this case.
I've tried various solutions to no avail. I can't use finish() in the main activity in onPause or onStop since that also exits the activity if the user for example enters the settings activity and that is not wanted behavior. I also tried variations of re-launching the starter activity with
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
but that doesn't seem to work either.
The thing that is weird is that when this unwanted behavior happens, onCreate/onResume/onStart is not called on the main activity. Still it shows! I'm guessing this is because it is stacked and since the startup activity is translucent, the main activity is shown through it.
Enable the android:noHistory attribute on your activity within your manifest:
<activity
...
android:noHistory="true">
...
</activity>
This will set the activity to be removed from the activity stack when it starts the next activity. The user will not be able to return to an activity that has android:noHistory="true".
See:
Activity Attribute: android:noHistory
I have splash screen in an application. When user is at home screen and presses the back button; an alert dialog with "Do you want to exit?" is appeared and if users slects OK then moveTaskToBack(true); is called: and the app will exit. But on immediate launch of the application the splash screen is not shown. How to show splash screen on every launch of the application?
A few pointers on how to make a splash screen.
Say we have SplashActivity and HomeActivity.
SplashActivity should be your launcher activity (in the manifest).
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
When you start HomeActivity you should call finish() on SplashActivity so that you wouldn't return to it upon clicking the back button.
In HomeActivity when you intercept the back button to show the dialog, don't call moveTaskToBack(true); on OK because this only moves your application to the background and doesn't terminate it.
You should call finish() instead. And since you already called finish on SplashActivity, the only activity left is HomeActivity, so your application will close.
Ideally You should not do that .You should respect user expectations for behavior of apps.When he clicks on your app it should start from where he left . If the app is killed due to memory constraints then it will start a fresh .
Still if you want to do it : Source : Close application and launch home screen on Android
Intent intent = new Intent(this, FinActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
Inside FinActivity :
call finish() in onCreate.
I think you SplashScreen is only shown when the App is starting.
So if the Acitivites aren't finished but just paused the SplashScreen will not be shown because the App wasn't really closed.
You can either finish the application when back button is pressed (Code: this.finish()) or check on every Activity's onResume()-Method if the App was in Background and show the SplashScreen if true.
I feel the only reason splash screens are ever necessary are for the launch of an application and if you want to load some information through a thread while doing so, but that is just my opinion.
Anyways, you should be launching your splash screen as the first activity in your manifest, then after you would launch an intent that will start your next activity?(The actual main). There is a great tutorial I used a couple of months ago when I was first dealing with these, It goes through it with you STEP by STEP.
I hope this helps :)
Tutorial I
Tutorial II