How to press back button in android programmatically? - android

In my app I have a logout functionality. If user clicks logout it goes to home screen. Now I am exiting my app by pressing back button. But what I want is I need to exit automatically(i.e Programmatically) as same like as back button functionality. I know by calling finish() will do the functionality. But the thing is it goes to the previous activity.

onBackPressed() is supported since: API Level 5
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
onBackPressed();
}
}
#Override
public void onBackPressed() {
//this is only needed if you have specific things
//that you want to do when the user presses the back button.
/* your specific things...*/
super.onBackPressed();
}

You don't need to override onBackPressed() - it's already defined as the action that your activity will do by default when the user pressed the back button. So just call onBackPressed() whenever you want to "programatically press" the back button.
That would only result to finish() being called, though ;)
I think you're confused with what the back button does. By default, it's just a call to finish(), so it just exits the current activity. If you have something behind that activity, that screen will show.
What you can do is when launching your activity from the Login, add a CLEAR_TOP flag so the login activity won't be there when you exit yours.

Sometimes is useful to override method onBackPressed() because in case you work with fragments and you're changing between them if you push backbutton they return to the previous fragment.

Call onBackPressed after overriding it in your activity.

you can simply use onBackPressed();
or if you are using fragment you can use getActivity().onBackPressed()

Simply add finish(); in your first class' (login activity) onPause(); method. that's all

For Kotlin users and working with fragments:
use finish() if you are navigating through activities, but for fragments you can remove the fragment by its fragment tag.
When navigating to the fragment use:
myFragment = myFragment(this)
parentFragmentManager.beginTransaction()
.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
.add(R.id.baseFragmentContainer, myFragment, "MYFRAGMENT")
.addToBackStack("myFragmentNameInStack")
.commit()
then, when tapping your particular button and simulate a "back button" pressing, do the following:
binding.someButton.setOnClickListener {
activity?.supportFragmentManager?.
popBackStack("myFragmentNameInStack", FragmentManager.POP_BACK_STACK_INCLUSIVE)
}
This will remove the fragment added by tag "myFragmentNameInStack", showing on screen the previous one.

public void onBackPressed() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
// make sure use in outside of onCreate Method

Related

How to close activity and go back to previous activity in android

I have a main activity, that when I click on a button, starts a new activity, i used the following code to do so:
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
The above code was run from the main activity.
Now in my new activity which is called by the main activity, I have a back button.
When I click on this back button I want my new activity to close and it must go back to the original main activity.
I have tried calling super.finish() and just finish() (from the new activity) but this then closes my entire application (including my main activity).
How can I just close the activity that is currently in focus, and then return to the main activity?
EDITED
The fact that my phone's back button also closes my entire app, leads me to think that i have started up the second activity incorrectly?
OK I have been looking,
I created a Settings Activity that uses the same manifest code and the same code to Start the activity.
For the settings Activity when I push the back button, it returns to the Main activity.
With the activity mentioned above in the main question it simply exits my entire app.
So the problem doesn't seem to be with the code to finish the activity but the activity itself.
I think you are calling finish() method in MainActivity before starting SettingsActivity.
The scenario which you have described will occur in following two ways:
EITHER
You have set android:noHistory = "true" for MainActivity inside AndroidManifest.xml which causes MainActivity to finish automatically on pressing the back key.
OR
Before switching to your 'SettingsActivity', you have called finish() in your MainActivity, which kills it. When you press back button,since no other activity is preset in stack to pop, it goes back to main screen.
You can go back to the previous activity by just calling finish() in the activity you are on. Note any code after the finish() call will be run - you can just do a return after calling finish() to fix this.
If you want to return results to activity one then when starting activity two you need:
startActivityForResults(myIntent, MY_REQUEST_CODE);
Inside your called activity you can then get the Intent from the onCreate() parameter or used
getIntent();
To set return a result to activity one then in activity two do
setResult(Activity.RESULT_OK, MyIntentToReturn);
If you have no intent to return then just say
setResult(Activity.RESULT_OK);
If the the activity has bad results you can use Activity.RESULT_CANCELED (this is used by default). Then in activity one you do
onActivityResult(int requestCode, int resultCode, Intent data) {
// Handle the logic for the requestCode, resultCode and data returned...
}
To finish activity two use the same methods with finish() as described above with your results already set.
if you use fragment u should use
getActivity().onBackPressed();
if you use single activity u can use
finish();
When you click your button you can have it call:
super.onBackPressed();
Button edit = (Button) view.findViewById(R.id.yourButton);
edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(this, YourMainActivity.class);
startActivity(intent);
finish();
}
});
try this code instead of finish:
onBackPressed();
I believe your second activity is probably not linked to your main activity as a child activity. Check your AndroidManifest.xml file and see if the <activity> entry for your child activity includes a android:parentActivityName attribute. It should look something like this:
<?xml ...?>
...
<activity
android:name=".MainActivity"
...>
</activity>
<activity
android:name=".ChildActivity"
android:parentActivityName=".MainActivity"
...>
</activity>
...
This closes the entire application:
this.finish();
You are making this too hard. If I understand what you are trying to do correctly, the built-in 'back' button and Android itself will do all the work for you: http://developer.android.com/guide/components/tasks-and-back-stack.html
Also, implementing a custom "back" button violates Core App Quality Guideline UX-N1: http://developer.android.com/distribute/googleplay/quality/core.html
I don't know if this is even usefull or not but I was strugling with the same problem and I found a pretty easy way, with only a global boolean variable and onResume() action. In my case, my Activity C if clicked in a specific button it should trigger the finish() of Activity B!
Activity_A -> Activity_B -> Activity_C
Activity_A (opens normally Activity_B)
Activity_B (on some button click opens Activity_C):
// Global:
boolean its_detail = false;
// -------
SharedPreferences prefs = getApplicationContext().getSharedPreferences("sharedpreferences", 0);
boolean v = prefs.getBoolean("select_client", false);
its_detail = v;
startActivity(C);
#Override
public void onResume(){
super.onResume();
if(its_detail == true){
finish();
}
}
So, whenever I click the button on Activity C it would do the "onResume()" function of Activity B and go back to Activity A.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if ( id == android.R.id.home ) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Try this it works both on toolbar back button as hardware back button.
Finish closes the whole application, this is is something i hate in Android development not finish that is fine but that they do not keep up wit ok syntax they have
startActivity(intent)
Why not
closeActivity(intent)
?
We encountered a very similar situation.
Activity 1 (Opening) -> Activity 2 (Preview) -> Activity 3 (Detail)
Incorrect "on back press" Response
Device back press on Activity 3 will also close Activity 2.
I have checked all answers posted above and none of them worked. Java syntax for transition between Activity 2 and Activity 3 was reviewed to be correct.
Fresh from coding on calling out a 3rd party app. by an Activity. We decided to investigate the configuration angle - eventually enabling us to identify the root cause of the problem.
Scope: Configuration of Activity 2 (caller).
Root Cause:
android:launchMode="singleInstance"
Solution:
android:launchMode="singleTask"
Apparently on this "on back press" issue singleInstance considers invoked Activities in one instance with the calling Activity, whereas singleTask will allow for invoked Activities having their own identity enough for the intended on back press to function to work as it should to.
on onCreate method of your activity
write the following code.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
Then override the onOptionsItem selected method of your activity as follows
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
finish();
}
return super.onOptionsItemSelected(item);
}
And you are good to go.
Just don't call finish() on your MainActivity then this eliminates the need to Override your onBackPressed() in your SecondActivity unless you are doing other things in that function. If you feel the "need" for this back button then you can simply call finish() on the SecondActivity and that will take you to your MainActivity as long as you haven't called finish() on it
it may be possible you are calling finish(); in the click button event so the main activity is closed just after you clicking the button and when you are coming back from next activity the application is exit because main activity is already closed and there is no active activity.
You have to use this in your MainActivity
Intent intent = new Intent(context , yourActivity);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
context.startActivity(intent);
The flag will start multiple tasks that will keep your MainActivity, when you call finish it will kill the other activity and get you back to the MainActivity
In case none of the above answers helped, I think this might help someone.
I was also having the same problem while pressing the built-in back button or my custom back button, the app closes without returning to the previous activity.
I was calling the second activity from the first activity's toolbar.
But in the starter activity I was calling this:
case android.R.id.home:
if (isActionMode) {
clearSelectingToolbar();
adapter.notifyDataSetChanged();
} else {
onBackPressed(); // This was making the activity to finish
}
break;
And this code to start the activity
case R.id.settings:
context.startActivity(new Intent(ShowAllDirectoriesActivity.this, SettingsActivity.class));
After removing 'case android.R.id.home' part, my activity was able to perform in a normal flow i.e getting back to the previous activity.
So check it if you are also using the same thing!
{ getApplicationContext.finish(); }
Try this method..

How to pass login screen activity when using onBackPressed

My app has three main activity.
Splash Activity
Login Activity
Menu Activity
When I start app, splash screen work with four second, then it goes login activity. If user login successfully then app goes menu activity. In my menu, there is logout button. If user clicks it, then app goes login activity.It works fine. But if users don't want to logout and click back button on devices, I want to exit from app directly. If I couldn't find solution for this problem, users have to go login screen, then splash screen and finally exit when they want to exit from app with using back button. Which solution should I use?
After the login has been successful you can call finish() on your Login Activity just after you started your Menu Activity. This effectively removes the Login Activity from the back stack.
Edit: As mentioned in the comment below, this also applies to your Splash Activity.
The solution provided by #Keyboardsurfer will work extremely good for you, but if you want to handle Back button event then use below code to get notified about back button event.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Log.d(this.getClass().getName(), "back button pressed");
// your code to handle back button event.
return true;
}
return super.onKeyDown(keyCode, event);
}
When the login is successful you should call your Menu Activity as follows:
Intent intent=new Intent(LoginActivity.this,MenuActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
this.finish();
I think you are checking in your Splash Activity if the user is logged via SharedPreferences, you should close your Splash Activity in the same way that you specify when you call any of the two activities.
You have to override the Activity's (Splash only) onPause method:
Here is how;
#Override
protected void onPause() {
super.onPause();
this.finish();
}
and in login activity call the finish method after the login stuff is done. Answer Edited (See the discussion below)
I don't think it would cause unexpected behaviour in other Activities, only the Splash Activity cause its its onPause() that was override not the others, and since splash is only called once i don't think it matters.

Back functionality in android

I am developing an application which starts from LoginPage. When user Login then he moves to Main Screen where grid view for different departments are present.
Every page of application except login page has a Footer which have different Icons like Home, logout, etc.
I want to add conditional back functionality using mobile back button. Some conditions are as follow:
1) LoginPage ---> Main Screen ---> On back user should log out and go to Login Page
2) Main Screen --> any department ---> Any Sub deprtment --> If user press Back button then go to back in same order
3) User is any where in application ---> If press home button from Footer ---> Comes to Main Screen --> No back functioality to go on previous page, It should follow condition 1.
4) If User on Login Page then he will exit from application on pressing Back Button
5) If User on main Screen then user should logout and go to Login Page on preseeing Back Button
I have tried with "noHistory=true" in Manifest and with Intent flags in Activity file.
Can any body suggest me best way to solved out it.
shouldn't be a problem, all you have to do is override the onBack function and add the logout process.
not a problem, the normal behavior of back buttons is exactly that.
DO NOT DO THIS!!! BAD BEHAVIOR.
normal behavior of back button.
that was step one.
this is used for exit from application on back press.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
System.exit(0);
}
return super.onKeyDown(keyCode, event);
}
if u want only back then remove System.exit(0) from above code .
By using this you can manage your all condition which one you want.
Use a stack globally to save screens order. Stack must be available in application level. And get the screen order when you click on back button. Write switch case for screen order and start that activity. that's it.
for example.
crate a class class MyStack{
//here declare a static stack
//create setter,getter method for assinging values to stack
}
when starting new activity assing screen value in stack with setter method
if you are starting a activity from main screen assign 1 into stack, you are starting sub screen assign 2 into stack.
when click on back get that value
switch(value){
case 1: //start mainscreen break;
case 2: //start sub screen break;
}
With what I understand, you cannot override the functionality of home button. By default, it minimizes your app with its current state, by calling onPause(). When you open the app again, onResume() is called and starts the app from where it was paused. As far as your back button functionality is concerned, most of the above answers are fine.
Try,
#Override
public void onBackPressed()
{
finish(); //finishes the current activity and doesnt save in stock
Intent i = new Intent(CurrentActivity.this, Login.class);
i.addflags(Intent.flag_activity_no_history);
startActivity(i);
}
Try this to trap events on the back button
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode == KeyEvent.KEYCODE_BACK) {
Intent Act2Intent = new Intent(thisActivity, Activity2.class);
startActivity(Act2Intent);
finish();
return true;
}
return false;
}
on each activity implement
OnBackPress().
Override it and add the functionality you want like logging out, clearing history stack and start new(previous) activity.
I think simplest approach may be to override back button in your "Main Screen" activity so that when back button is pressed you can do :
1. Executing log out logic:
2. Explicitly call your Login Page
This may give the behavior you are looking for.
On how to override back button, you can refer to this link:
http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html
Hope this helps!

How to prevent going back to the previous activity?

When the BACK button is pressed on the phone, I want to prevent a specific activity from returning to its previous one.
Specifically, I have login and sign up screens, both start a new activity called HomeScreen when successful login/signup occurs. Once HomeScreen is started, I want to prevent the users from being able to return to the login or sign up screens by pressing the BACK key.
I tried using Intent.FLAG_ACTIVITY_NO_HISTORY, but since the application has Facebook integration, when the 'Login with Facebook' is used, Facebook should return to the initial login screen, therefore I should keep a history of these activities.
I thought of overriding the behaviour of the BACK button on HomeScreen to directly finish an application when the button is pressed and I used
#Override
public void onBackPressed() {
finish();
}
but that also does not work.
My suggestion would be to finish the activity that you don't want the users to go back to. For instance, in your sign in activity, right after you call startActivity, call finish(). When the users hit the back button, they will not be able to go to the sign in activity because it has been killed off the stack.
Following solution can be pretty useful in the usual login / main activity scenario or implementing a blocking screen.
To minimize the app rather than going back to previous activity, you can override onBackPressed() like this:
#Override
public void onBackPressed() {
moveTaskToBack(true);
}
moveTaskToBack(boolean nonRoot) leaves your back stack as it is, just puts your task (all activities) in background. Same as if user pressed Home button.
Parameter boolean nonRoot - If false then this only works if the activity is the root of a task; if true it will work for any activity in a task.
I'm not sure exactly what you want, but it sounds like it should be possible, and it also sounds like you're already on the right track.
Here are a few links that might help:
Disable back button in android
MyActivity.java =>
#Override
public void onBackPressed() {
return;
}
How can I disable 'go back' to some activity?
AndroidManifest.xml =>
<activity android:name=".SplashActivity" android:noHistory="true"/>
There are two solutions for your case, activity A starts activity B, but you do not want to back to activity A in activity B.
1. Removed previous activity A from back stack.
Intent intent = new Intent(activityA.this, activityB.class);
startActivity(intent);
finish(); // Destroy activity A and not exist in Back stack
2. Disabled go back button action in activity B.
There are two ways to prevent go back event as below,
1) Recommend approach
#Override
public void onBackPressed() {
}
2)Override onKeyDown method
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK) {
return false;
}
return super.onKeyDown(keyCode, event);
}
Hope that it is useful, but still depends on your situations.
Since there are already many great solutions suggested, ill try to give a more dipictive explanation.
How to skip going back to the previous activity?
Remove the previous Activity from Backstack. Simple
How to remove the previous Activity from Backstack?
Call finish() method
The Normal Flow:
All the activities are stored in a Stack known as Backstack.
When you start a new Activity(startActivity(...)) then the new Activity is pushed to top of the stack and when you press back button the Activity is popped from the stack.
One key point to note is that when the back button is pressed then finish(); method is called internally. This is the default behavior of onBackPressed() method.
So if you want to skip Activity B?
ie A<--- C
Just add finish(); method after your startActvity(...) in the Activity B
Intent i = new Intent(this, C.class);
startActivity(i);
finish();
finish() gives you method to close current Activity not whole application. And you better don't try to look for methods to kill application. Little advice.
Have you tried conjunction of Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_NO_HISTORY? Remember to use this flags in Intent starting activity!
Put finish() just after
Intent i = new Intent(Summary1.this,MainActivity.class);
startActivity(i);
finish();
If you don't want to go back to all the activities on your application, you can use
android:launchMode="singleTask"
Learn more here: http://developer.android.com/guide/topics/manifest/activity-element.html
paulsm4's answer is the correct one. If in onBackPressed() you just return, it will disable the back button. However, I think a better approach given your use case is to flip the activity logic, i.e. make your home activity the main one, check if the user is signed in there, if not, start the sign in activity. The reason is that if you override the back button in your main activity, most users will be confused when they press back and your app does nothing.
This method is working fine
Intent intent = new Intent(Profile.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
#Override
public void onBackPressed() {
}
When you create onBackPressed() just remove super.onBackPressed();and that should work
Just override the onKeyDown method and check if the back button was pressed.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
//Back buttons was pressed, do whatever logic you want
}
return false;
}
Put
finish();
immediately after
ActivityStart
to stop the activity preventing any way of going back to it.
Then add
onCreate(){
getActionBar().setDisplayHomeAsUpEnabled(false);
...
}
to the activity you are starting.
AndroidManifest.xml
<activity
android:name=".welcome.SplashActivity"
android:noHistory="true" // just add this line
android:exported="true">
</activity>

activity and back button

on the ui i am having a back button displayed. there are three activities say A,B,C. Activity A is splash screen. Activity B is home screen. Activity C is edit screen. on edit screen only i am having a back button. when the user press that i open the activity B which also contains some sort of http request and response. i want that the previous activity from stack should get displayed? how can i achieve that? i am able to open previous activity on pressing the back button of device?
when i press back button on device there doesnt goes any http request? i want to achieve this behaviour when i press the ui back button.
I think in your back button you may call the intent for activity B and your http request and response code is in onCreate function
But the back button on device will not call onCreate
There is Two solution for this
One as Macarse say, listen onKeyDown
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Intent i = new Intent(ActivityC.this,ActivityB.class);
startActivity(i);
return true;
}
return super.onKeyDown(keyCode, event);
}
And the second method is to write you code on onStart of ActivityB
protected void onStart() {
//http request and response code
}
This onStart will call all the time this when ActivityB open
Hope this help you
First of all, it's not good to have a back button displayed in the ui. Every android device has a back button in it.
If you want to handle the back button in a different way, check this link.
The solution with onKeyDown might work but using onBackPressed is much easier in my opinion.
You can intercept the Back-key with following override:
#Override
public void onBackPressed()
{
//logic here, for example an intent
Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);
}
And for the other way around super.onBackPressed(); returns to previous Activity in history.

Categories

Resources