block back button for ceratin activities - android

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();
}

Related

After closing activity Android goes back to activity that should no longer b in the stack

I have activity A->B in the stack, and to launch activity C, I call
Intent starter = new Intent(context, MainActivity.class);
starter.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(starter);
This all works fine, Activity A and B both have OnDestroy called. If I press the hardware 'back' button now, the activity appropriately finishes and is hidden. The problem is now however, if I return to the application through the application by clicking the hardware recent apps button, it will return to Activity A. Activity was destroyed and not in the stack. In the manifest, none of activities have had a android:launchMode set, so they are on default.
The only other possible piece of relevant information is that there is an Activity X that is a launcher Activity that is android:launchMode="singleInstance" and it launches activity A, that being said, it gets destroyed and it shouldn't be in that activity stack anyways.
While pressing back button While in Activity C may called onDestroy() of actvity C.
please insert logs to see whether it is called or not. This is the only reason why your activity A launch again.
please refer Android Back button calls ondestroy?
please let me know if these not work for you.
The hardware back-button can be overwritten by the follwing code :
#Override
public void onBackPressed() {
//put Intent to go back here
}
You could just overwrite it with your code written above

Find out where back button press is going

When user presses a back hardware button in my activity he will either be taken to a previous activity or to the home screen (depends on how he got into that activity in a first place). Is there a way to determine where exactly user will be taken inside of onBackPressed method?
In your home and previous activity when you start the intent to go to this activity, you can add an extra to the Intent.
Intent.setExtra("PREVIOUS_ACTIVITY ", "HOME")
Then you can check in your activity onBackPressed() if it came from the home or another activity.

Android - Back Press on the activity started from the notification bar

[UPDATE BELOW]
I have application with multiple activities. I have issues in handling the activity started from the click of notification item.
Below is the app structure:
For normal flow/working, user start the application and Activity A being the launch Activity, it starts. User could navigate to activity B from activity A (i.e. A -> B)
On click of back button on B, activity A is displayed.
However, the issue is with starting activity from the click of notification. Below is the flow:
User clicks on the notification
Activity B is started
On click of back button on activity B, it doesn't start activity A (which is understand) but it doesn't kill the activity B either (moves activity B to background). What I want is, if the activity B is started by the notification click, then back press on B should either takes user to Activity A or kill activity B.
Any pointers in this regard would be appreciated.
[UPDATE]:
Strange but, this is what is happening now.
1. Overridden the onBackPressed event in activity B.
2. In this method, finish() is called.
If the application is already running and on click of notification, activity B is displayed and then on back press, the activity B is finished. However, if the application is not running, notification click starts the activity B and on back press, it does call the backpress event and finish() method but the activity doesn't finish and goes in the background. This is really weird. Or may be I am missing some details :((
Thanks
Shrey
Override in onBackPressed().
#Override
public void onBackPressed()
{
finish();
super.onBackPressed();
}
But, user can directly got to activity B, if he/she start's your app from recent apps. To avoid this, add below to your ActivityB in Manifest. If you are navigating to ActivityC from here, pressing back in ActivityC will not return to ActivtyB so override onBackPressed() and relaunch ActivityB via Intent.
<activity
android:name="ActivityB"
android:clearTaskOnLaunch="true"
android:excludeFromRecents="true"
android:finishOnTaskLaunch="true"
android:noHistory="true"/>
follow this flow:-
1- check isLauchFrom in oncreate() of Activity A.
2- If isLaunchFrom Notification then navigate on Activity B.using startActivityFor Result.
hope this will help you ,If any Confusion send comment.
#Override
public void onBackPressed() {
Intent returnIntent = new Intent(B.this,A.class);
B.this.finish();
startActivity(returnIntent);
}
Try like this

How to destroy an Activity so it doesn't show up when the back button is pressed?

I have two Activities, A and B. Here is a normal scenario: A is running, then sends an intent to B. A is paused, and B displays. When the user presses the back button from B, B us destroyed and the user is returned to A.
However, there is a case where B needs to re-create itself. To do this, I call finish() and then startActivity() on B and that works fine. But then, when I click the back button, it shows B again, and so I need to click the back button once more to get back to A.
How can I re-start B, but still be able to press the back button only once to return to A?
The following will dispose of the current activity while launching the next intent:
Intent launchNext = new Intent(getApplicationContext(), NextActivity.class);
launchNext.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(launchNext);
To override the back button, use the following:
#Override
public void onBackPressed() {
super.onBackPressed();
this.finish(); // or do something else
}
This can be solved by taking a closer look at your intent flags. Check out http://developer.android.com/reference/android/content/Intent.html and they give more information about what lifecycle you are shooting for.
Also, don't forget that you can override the back button functionality.
This may be helpful in case you want to manage your life cycle more closely.
For example, you can also make sure to go back to A if back from B. And close your app if back on A.

Android reuse activity from activity stack and clear it

I have application with main activity and some more.
On each other activity there is the logo of the application.
When user presses the logo button, I want to get back to the main activity.
I do not want to create new intent, since activity aready on the activity stack.
How can I get it - use the one on the stack?
How can I clear the whole activity stack, so back button will actually exit from the application instead of getting back to the previous activity?
Yoav
I do not want to create new intent, since activity aready on the activity stack.
If you start an activity (via intents or any other way) which was already started and is on the stack , then Android just takes that same instance of the activity and places it on top of the stack. A new instance is not created. Ofcourse this happens if you did not manually kill the activity (by calling finish() in it).
How can I clear the whole activity stack, so back button will actually exit from the application instead of getting back to the previous activity?
Its not recommended to override the back button to quit the application in every activity(Unless your app has strong reasons to do so). generally the app should let the user go back to the previous activity when he presses the back button (which is what a user might be expecting).
If you still would like to quit with the back button then you can override the back button function and launch the intent that leads to the home screen:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I faced a somewhat similar problem. The following link might be helpful for you:
clearing stack of activities with just one press
Its very simple.
Don't call finish() on Home/Main activity.
For Ex: Say you have 4 activities.. If your requirement is like this .. Act1-->Act2-->Act3-->Act4-->Act1 . So, don't call finish() on Act1. But call finish() on Act2, Act3 while you are going to other activity. So when you click on logo in Act4, just call finish(). So, automatically you will come back to Act1 which is your Main activity.
If you have logo in Act2, Act3 also then call finish() on click of logo to go back to Main. But remember to call finish() on Act2 while you are going from Act2 to Act3

Categories

Resources