Android - How to Override the "Back" Button So It Doesn't Finish() My Activity

Android - How To Override the Back button so it doesn't Finish() my Activity?

Remove your key listener or return true when you have KEY_BACK.

You just need the following to catch the back key (Make sure not to call super in onBackPressed()).

Also, if you plan on having a service run in the background, make sure to look at startForeground() and make sure to have an ongoing notification or else Android will kill your service if it needs to free memory.

@Override
public void onBackPressed() {
Log.d("CDA", "onBackPressed Called");
Intent setIntent = new Intent(Intent.ACTION_MAIN);
setIntent.addCategory(Intent.CATEGORY_HOME);
setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(setIntent);
}

Android: how to quit an activity without closing it when click on back button?

On you subActivity onBackPressed() add this

@Override
public void onBackPressed() {
Intent i = new Intent(SubActivity.this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

startActivity(i);
}

on mainActivity :

  private void openSubActivity() {        

Intent intent = new Intent(MainActivity.this,SubActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);

}

how to override action bar back button in android?

I think you want to override the click operation of home button. You can override this functionality like this in your activity.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
break;
}
return true;
}

Android: Previous activity returns on back button press after finish()

When you press back button, the instance of MainActivity is destroyed.

And then you come back to this task stack again, the LogoActivity is your default Activity so the system creates one instance of it for you.

You can make the MainActivity the default Activity in manifest.xml

<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

And start LogoActivity in the onCreate method of MainActivity so the user will see LogoActivity first.

After 3 seconds, finish the LogoActivity.

How to prevent going back to the previous activity?

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.

Prevent ActionBar back button to recreate the MainActivity

The ideal scenario is, when are you in the SecondActivity (I take it that, this means that you are in Edit mode), and you press the device back button or ABBB, you show a subtle alert to the user saying "do they really want to dismiss the editing", or go ahead and apply the edit done as in the Contacts application.

So that being said, if all you require is to finish() the activity on press of ABBB, the code that you shown above should work fine (though you need to put return true; after finish()). It works for me. Try the below one in your activity.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed(); // or finish();
return true;
}
return super.onOptionsItemSelected(item);
}

I put onBackPressed(); because your ABBB with now imitate your device back button.

problem in activity finish() and on pressing back button in android

There is a better way:

You should not call finish() when going from one activity to another. If the memory is needed, the OS will clear it for you, and when you press back it will work as needed. Just make sure your lifecycle functions are correct (you clean up big chunks of memory hogging stuff when you loose focus etc), and you'll be a much happier coder .

Android Terminate application on-click and back button both in each activity

when you intent activity from splash screen need to add FLAG_ACTIVITY_NEW_TASK,FLAG_ACTIVITY_CLEAR_TASK,FLAG_ACTIVITY_NO_ANIMATION

  Intent mIntent = new Intent(context, mClass);
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
mIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
context.startActivity(mIntent);
finishActivity();

1)declare this variable globally


private boolean doubleBackToExitPressedOnce;
private Handler mHandler = new Handler();

2)Then Implement this 3 below method
--------------------------------
@Override
public void onBackPressed() {

if (doubleBackToExitPressedOnce) {
super.onBackPressed();
finish();

}

this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();


mHandler.postDelayed(mRunnable, 2000);

}

@Override
protected void onDestroy() {
super.onDestroy();

if (mHandler != null) {
mHandler.removeCallbacks(mRunnable);
}
}
@Override
protected void onResume() {
super.onResume();
this.doubleBackToExitPressedOnce = false;

}

private final Runnable mRunnable = new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce = false;
}
};


Related Topics



Leave a reply



Submit