-1

I have tried to use the code below as described in other questions to Back button act like Home button, but it is not working:

@Override
public void onBackPressed() {
moveTaskToBack(true);
}

I don't have points to write a comment on others question, because of this I have opened this one.

1st issue: The HOME button is making the app quit. Only after the smartphone is reseted on the first time, the HOME button put the App in background (NOT quit)

2nd issue: I have tried to make the BACK button to act like HOME button with the code above, but the BACK button is only disabled.

. After the 1st SUCCESSFUL Login and Twitter Authentication, when the user clicks on the BACK button OR HOME button (Android buttons), the App has to stay in background (NOT quit).

What am I doing wrong?

Thanks

The App: http://play.google.com/store/apps/details?id=com.xranky

FIXED issue 2: with @zilk code below

FIXED issue 1: you have to insert the code below on the login/start screen

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0)  { 
       // Activity was brought to front and not created, 
       // Thus finishing this will get us to the last viewed activity 
    finish(); 
    return; 
    } 

     // Regular activity creation code... 
    } 

Source: How to return to the latest launched activity when re-launching application after pressing HOME?

Community
  • 1
  • 1

3 Answers3

1

The below code doesn't finishes your activity but that takes your application to the background like home button.

 public void onBackPressed() {    
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    startActivity(intent);
}
ziLk
  • 3,120
  • 21
  • 45
  • it didn't work. When I click on the HOME button, the app goes to background. After click on the app icon again, open the login screen (not the last screen before goes to background). – Alex Barros Sep 25 '16 at 08:34
  • @AlexBarros This is the answer of the question. " open the login screen (not the last screen before goes to background). " is another issue that you should control inside of the login activity. Maybe you can store the latest activity before it is closed, and open it when you need it. – ziLk Sep 25 '16 at 08:41
  • you are right. With the code above, when I click on Back or Home button, the apps goes to background. Afterward, when I click on the app icon, the app open the 1st screen (login screen) and when I click on the Back button, the app goes to the last screen before the background. I am looking for a good solution for store last activity. – Alex Barros Sep 25 '16 at 16:14
  • @AlexBarros you can follow the link https://inthecheesefactory.com/blog/fragment-state-saving-best-practices/en – ziLk Sep 25 '16 at 16:34
  • issue 1 fixed. See above. – Alex Barros Sep 25 '16 at 19:45
0

Use the below code for this.

public void onBackPressed() {
              super.onBackPressed();
            }
ziLk
  • 3,120
  • 21
  • 45
Joyal C Joseph
  • 290
  • 2
  • 12
  • When I insert the code above, the issue 1 with HOME button is fixed (don't need to reset the smartphone to app goes to background). However, the BACK button makes the app quit. – Alex Barros Sep 25 '16 at 08:27
0

Inside your Activity you have to override onBackPressed().

 @Override
        public void onBackPressed() {
            super.onBackPressed();
            Utils.redirectToHomeActivity(this);
        }

Utils.java

public static void redirectToHomeActivity(Activity activity) {
            if (activity == null || activity.isDestroyed())
                return;
            if (!activity.isTaskRoot()) return;//Return whether this activity is not in the root of a task.

            Intent intent = new Intent(activity, MainScreenActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);//this is use to start new activity
            activity.startActivity(intent);
        }
Abdul Rizwan
  • 3,904
  • 32
  • 31