Exit application programmatically - android

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.

Related

Creating a back button to a previous activity (CONCEPT ONLY)

Before I say anything else, I am not asking how to make this work for my program, I want to figure it out myself. I have already seen one billion snippets of codes and answers. But I think I am not understanding the concept behind it and would like to verify if my understanding is correct.
Creating a back button and making it work:
1) Find and edit the xml for the action bar so show a back button.
2) Use the buttons id to create OnClickListener.
3) In OnClick, use intent to previous activity.
Is it not this simple? What are the proper steps conceptually for make this work?
That might work, but it wouldn't be the norm.
Depending on how you launch an Activity, simply calling finish() on an Activity will take you back to the previous one.
easier to
1) use AppCompatActivity
2) enable homeAsUp (back button)
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true)
Android activities are stored in the activity stack.
So simply by calling the finish() method if you started the activity with startActivity()
this.finish()
or if you opened the new activity from another activity with startActivityForResult()
call the finishActivity() method from your code on the click of the button
this.finishActivity()
And it'll take you back to the previous activity.
Well..... I don't know this answer would be helpful but, you said
3) In OnClick, use intent to previous activity.
However, how about thinking this situation?
If there is fragment and you press 'back button' in there, you should go before fragment not before activity. :)
You said you want to figure it out yourself so, I won't say more(about codes) as you wanted. Good Luck!

Android custom layout(alert) in service (similar to messenger)

My question is simply how can i do a dialog pop up from other application.
I search around and many answer is NO.
However, the messenger like Whaxxapp is exactly doing this. It can pop up an instantly reply while i am playing game, taking picture and etc. (like pausing)
And i had do some research and works,
i am able to create a service which start from my activity (activity A).
And the service is registered a contentobserver, when the observer onchange is fired, it can launch an activity(pendingactivity B) and the activity B which i had set the theme transparent and look like a dialog popup. however, it will also change the background back to my activity A and with the popup as well.
What i want to do is not go back to my activity A and just pop up (or launch the pending activity B) when i receive an onchange. (same as Whaxxapp pop up message with interaction)
Thanks so much.
You should start Activity B to new task. So when you press back activity A won't be shown. Add Intent.FLAG_ACTIVITY_NEW_TASK flag to Activity B intent.
I'm guessing, but I think the Whatsapp "Popup" is actually an Activity styled to look like a Dialog.
You can do this changing an Activity's theme to:
<activity android:theme="#android:style/Theme.Dialog" />
You can then add Button's, etc, just like a normal Activity to add interaction.

Android how to create activity only if it wasn't created yet

I have a problem cause when I go to different activities using startActivity function,
they always get created from scratch. Even if activity A was visited before, when I go to activity B and then A again, activity A is created again.
The problem is with back button, cause if I go to Activity A then B then A and then B,
in order to close the application I have to press back button 4 times.
I guess that it shouldn't act like it and user should be able to go to activity A when first pressed back button and the second press should close the application.
How to solve this issue?
Greetings
If you have activity transitions like:
Activity A -> Activity B
Activity B -> Activity A
and you want the user to go back to the same instance of Activity A in this case, maybe you just need to call finish() in Activity B after you call startActivity() for Activity A?
If this isn't helpful, please give us more information about what you are trying to do.
make sure you implement onSaveInstanceState and be prepared to restore your activity from a Bundle in onCreate. that's how you re-establish where you were when you return to an activity.
add launcheMode="singleTask" to your activity in the manifest
You need to set FLAG_ACTIVITY_SINGLE_TOP to your intent for launching activity A. Doing so will cause your previously created activity to re-use. Make sure you do handle your afterwards intents in onNewIntent method. For more info.
You need to set the flag FLAG_ACTIVITY_REORDER_TO_FRONT when you start activity A from B or vice versa, like
i = new Intent("....ActivityAorB");
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
I've tried solutions proposed so far, however they didn't do it for me.
What did however, is using flag FLAG_ACTIVITY_CLEAR_TOP while starting activities.
Thanks for pointing me in the right direction though.

Avoiding Blank Screen when it Raised from Intent in Android

I have an application that raises from service on scheduled time. In that service, i have displayed my Activity using Intent. It works perfectly. But, when i go back, the activity is finished. But, one blank screen is remaining there? How can i avoid this blank screen. I need my application's Activity instead of blank screen. Anyone Guide me.
#Špĸ remove this line: i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
take a look at my post if you wish to open a dialog via your service: Creating global dialogs
I don't like that you finish an activity that you go back to. You have multiple of different alternatives:
Using fragments instead of activity intents.
Set flag to indicate the activity is done so it will close when you go back.
You could also try to work with the stack, look at this: https://stackoverflow.com/a/4038637/969325
Without seeing the code it is very diffcult to find out what is going wrong in your code but still I am making some effort to help you.
Add android:theme="#android:style/Theme.Translucent" to your activity tag in your manifest file and then try if it works or not.
Have you tried setting FLAG_ACTIVITY_NO_HISTORY in the intent you use to start the AlertDialog activity or launchMode="noHistory" in the manifest.
The AlertDialog activity may very well be the blank screen you're seeing as no content view is set and the only thing a user sees is an alert dialog so once the alert dialog is dismissed and the app goes back to this screen it would be blank.
Before press back button, you have to save the state of the activity and restore the activity while come front. This is easily done by onRetainNonConfigurationInstance() and getLastNonConfigurationInstance() method. Please refer this link.

android, starting and exiting activities

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.

Categories

Resources