Suppose i am having an activity (let's say activity1) and i have given a command for doing some long process. Mean while i am starting another activity (activity2), during this time if the activity1 finishes the process and shows the result in an alert box, then how can i make this alert box of activity1 appear over activity2? What i have noticed is that, the alert box of activity1 is only visible when i move back to activity1. Is there any way to do this? Ignore if the question is irrelevant, as i am just a beginner in android.
Before using two activity, you have to save the state of the activity and restore the another activity while come front. This is easily done by onRetainNonConfigurationInstance() and getLastNonConfigurationInstance() method. Please refer this link.
Related
is it possibile to get the current Activity stack from code?
Here is my use case:
the user navigates from Activity to Activity
A -> B -> c -> D -> ...
some of the Activity will finish after sending the user to the new screen, some others won't
I need to check if pressing the BACK button will bring the user to the Activity A or any other because in the currently shown Activity I have a button that finishes the Activity and the icon of the button is different weather you'll be taken to tha Activity A or another Activity.
Thanks in advance,
Bye,
Maurizio
My impression is that the answer to the literal question is "no" - you cannot access that. But there's probably another way to accomplish what you need.
If these are all your activities, why not put an extra in the Intent (when going in the forward direction) which says which activity it is coming from.
You can then decide which back button image to display based on checking that extra, which should tell you what the previous activity was (if it was one of yours - if it's empty you have to assume it came from somewhere else in the system)
Here i have a few activities that consist different menus in my app..
The problem is that i want to add a are you sure popup box to exit the current menu and return back but calling finish() method on the click event of yes button of popup box causes all activities to terminate and app exits...
I want to make a way to terminate only the foreground activity and
return to last activity programatically (i.e without using back key)
Can u post some source code regarding how you start you new activities? Are you starting multiple activities at all? finish() method only finishes the current activity and not the entire stack of activities, thus the system automatically brings to front the previous activity from the stack. I can't understand your question please provide some further details.
I have been searching for an answer but I couldn't find a proper one. The question is that I have a dialog themed activity on top of a normal activity. I would like to force the users to either read and click "OK" to the themed activity which will then transfer them to another dialog themed activity for some further questions or cannot enter the application. So, I would like to exit the application, on back press, and not just finish the themed activity that will reveal the content of my app. How is that possible?
If you start your dialog activity with startActivityForResult() you can send back the result RESULT_CANCELED from the dialog, and upon receiving this (in your main activity) you call finish().
finish() will do the perfect job for you ;)
but make Dialog not cancelable..
Edit (after problem description clarification):
As others said StartActivityForResult could work, with additional trick.
Because of the way you design your App (DialogActivity1->DialogActivity2), it might help to add following line in AndroidManifest file, for all your special dialog-look activities:
android:noHistory="true" or to set flag for intent Intent.FLAG_ACTIVITY_NO_HISTORY before u start DialogActivityN.
Both lines (from manifest or code) will make this actitivies not to stay on android stack, so when your MainActivity get result back, it will be result from last DialogActivity and than depending on result recieved you can either finish() or continue with execution of MainActivity..
when u start activities like this there is no need to call finish() to destroy them, u just start new activity and they will be gone from stack. Of course, in your case, last DialogActivity u will start with StartActivityForResult() and as I explained in previous paragraph MainActivity will do something based on received results.
However making user goes through these dialogs several times at the beginning application, is not something I would consider good practice and it can make your user just give up and go for some less annoying app. (don't get this wrong, it's just my advice to rethink about concept)
Hope you will solve it ;) Cheers
If you know about ActivityforResult then way is easier for you, First you need to start the dialog activity with method startActivityforResult... and then when dialog activity get close by back button you have to close it by Set result. In OnactivityResult method of start activity have to detect the same and close the same if setResult is not as according. Hope you got the point.
I have not really understood the handling of activities and the stack.
I have 3 activities, A - a splashcreen, B- a menu and C another Activity. I start the splash and exits it after a while when the menu is started.
In code I handle them all like this:
startActivity(new Intent(this, ContactInfoMenu.class));
finish();
Now, if I start the app and goes A-B-C, when I hit "Back" in C screen I jump back to B-the menu. Another "Back" exits the application, just like I want.
BUT .. if I go A-B-C-B-C - the "Back" button in C screen exits the whole app instead of getting me back to the B screen?
Why is that? It does like that in all my "subscreens", I can only enter them once, if I enter them a second time the "Back" button exits the app. And I have not tried to catch the "Back" action anywhere? Shouldn't I always call "finish()" when I start a new activity?
Regards
Finish is good for leaving the current activity and going back to the previous one. Otherwise, try to avoid calling finish() if you can help it.
There are a set of flags that you can pass when you start an activity that do a better job of determining how that activity behaves on the stack. These include:
FLAG_ACTIVITY_NO_HISTORY - your activity will not remain on the stack after another activity covers it.
FLAG_ACTIVITY_CLEAR_TOP - a good way to pop off a bunch of activities when you need to "go back" to a certain activity.
Many of these flags can be set in the manifest. Reading up on them will give you a better idea about "The Android Way".
Basically, You don't need to call finish() every time you go to another activity. If system is low on memory it will close your activity instance by itself.
finish() is more often used when yor are inserting some information in one page and then moving on to some other page. In that case, you might need to fininsh your first activity.
But in case where you need to shuffle between views, you must not use a finish() function, because it will cause the whole application to finish.
Try using back button of your own in your views to shift between activities, where you can move to any other activity of your application or even to the Main Screen.
I've got an activity, call it A, that when initially opened I would like to programmatically open a another dialog theme activity, B, over the top. The user would select some info from the dialog activity and return to activity A. When resuming to activity A at some later point the data set would be available, so the dialog would not need to be shown.
The problem is attempting to start the dialog activity from activity A's onResume or onCreate methods causes strange and undesirable behaviour. The task appears to kind of freezes for a good few seconds and then the dialog is displayed correctly, but activity A isn't shown behind.
Thanks
Peter
check this:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/CustomDialogActivity.html
i think u should create activity as a dialog.then it helps
u can set style and theme for ur activity by this..