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.
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 application with two activities A and B. On clicking on list item in activity A it takes me to the activity B.
For the first time the activity works fine.
Now I have a broadcast reciever which throws me a notification , on clicking on that notification it takes me to the activity B.
The problem is when i am allready on activity B and the new notification comes and when i click on it it relaunches the same activity.When i press back the activity is not destroyed properly as i am getting logs of previous Activity B when i am currently on Activity B.
I have tried out allmost all suitable flags like Intent.Flag_Clear top etc...
But it isnt helping me out.
My activity sometimes works fine and sometimes doesnt work properely.
Please help me out stuck on this problem since a week
I think the launchmode attribute in your Manifest will do the trick. See http://developer.android.com/guide/topics/manifest/activity-element.html and scroll down to the launchmode.
singleTask or singleInstance sounds like it is what you want
You can change the launch mode of your activity on the manifest file. See this activity attribute: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
in AndroidManifest.xml use android:noHistory="true" in your activity. This will destroy your activity B destroyed whenever you leave it.
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.
I have the following problem, and I would like to design the implementation to make sure I will not encounter any issues.
At application startup, I need to do one (and only one) server request to keep the phone up to date. Then I enter into the main activity.
My biggest issue is the Back button with Android, which can potentially bring back my stack to the very first activity, ie, the one that does server synchronisation.
I have thought of the following implementation:
I launched the Main activity straight away
In the onCreate() of the Main Activity, I launch the synchronisation process ... with some background logos, progress bar, etc etc...
Upon completion of the synchronisation, i call finish() function on my Activity.
Will I then return the onCreate(), or straight to onResume() of the main Activity?
Does this implementation make sense?
You can continue doing the same but for the first activity where you do the synchronization thing, make it a noHistory Task. http://developer.android.com/guide/topics/manifest/activity-element.html#nohist
Or in your First Activity, after you call startActivity(MainActivity) you can call finish() and by doing which your FirstActivity will be removed from backstack and then MainActivity will remain on top of the stack.
Hope that helps.
UPDATE
I mean to say is, let FirstActivity be your first activity and you start MainActivity from FirstActivity. After you call startActivity() in your FirstActivity, call finish() in the very next statement. This is completely acceptable.
UPDATED
to prevent your first Activity from being viewed again, you just need to add the following line to you Activity declaration
<activity
android:name=".FirstActivity"
android:noHistory="true" />
Using the noHistory tag will remove your Activity without the need to do it your self programmatically
I am trying to create an application that defines two activites. The first activity pretty much runs all of the time. The second activity requires the user to authenticate to use the device.
Most of the time this application works fine. However, I am having problems figuring out how to force the second activity to the top of the window stack. The code calls startActivity passing in an intent to start the second activity. The problem is that when another application is running (e.g. a web browser), the second activity is not on top. When the other application exits, the second activity is visible to take input from the user.
Here is the activity definition for the second activity that I want to have always on top when started:
<activity android:name=".Authenticate"
android:launchMode="singleTop"
android:configChanges="orientation|keyboardHidden">
</activity>
It is my understanding that when start activity is called, it will put the new activity on top. Is there something that I am missing? How can I make the authentication activity come to the top when it is started?
Well AFAIK, you cannot force your Activity to stay on top all the time. If some other process (say Web Browser) creates an Activity after you have created yours, then that processes Activity will have focus and not yours.
whenever a new activity starts it is on top of the stack and if another application is running then it will be on the top of the stack and if you want your activity on top of the stack then you should stop other application being launched....Is there something I am missing then let me know..