-1

Assume you are in Activity A and press home button now your app will goto background. Now long press your home button and you can see recent's app. If I click my app it should take to particular activity not Activity A.

Selvakumar
  • 84
  • 7
  • Why don't you make your prefered 'starting' Activity your starting Activity (MainActivity)? – Kody Jan 10 '15 at 10:13
  • override `onResume` of activity and start witch activity you want, set one flag in `onCreate()` and `onPause()` – Shayan Pourvatan Jan 10 '15 at 10:14
  • How can I identify user has pressed home button and coming back from recent's? same activity lifecycle methods are called when moving from one activity to another @shayanpourvatan – Selvakumar Jan 10 '15 at 10:20
  • when you back to activity from home or other activity `onResume()` has been clled, you can handle in that method – Shayan Pourvatan Jan 10 '15 at 10:22
  • How to figure out they are coming back from recent or other activity? @shayanpourvatan – Selvakumar Jan 10 '15 at 10:26
  • http://stackoverflow.com/questions/4414171/how-to-detect-when-an-android-app-goes-to-the-background-and-come-back-to-the-fo/9876683#9876683 – Shayan Pourvatan Jan 10 '15 at 10:32

2 Answers2

1

With reference of this post - How can I detect user pressing HOME key in my activity? have implemented below logic.

Detect Home button press by using above post and store a flag in preferences. While launching app from Recent's list by first OnRestart() method will be fired so check home button pressed flag and launch particular activity.

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        if (isApplicationSentToBackground(this)) {
            // Do what you want to do on detecting Home Key being Pressed
             preferences.setHomeButtonPressed(true);
        }

    }

    @Override
    protected void onRestart() {
        super.onRestart();
    // Do what you want to do on detecting app launching from recents section
        if (preferences.isHomeButtonPressed()) {
            Intent i = new Intent(this,
                    ParticularActivity.class);
            startActivity(i);
            preferences.setHomeButtonPressed(false);
        }

    }

    public static boolean isApplicationSentToBackground(final Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            ComponentName topActivity = tasks.get(0).topActivity;
            if (!topActivity.getPackageName().equals(context.getPackageName())) {
                return true;
            }
        }
        return false;
    }
Community
  • 1
  • 1
Selvakumar
  • 84
  • 7
0

I think is a strange requirement, but you could do something like this:

@Override
public void onResume() {
   startActivity(new Intent(this, TargetActivity.class));
}   

onResume() is called right before your app is in state 'running'

enter image description here

Kody
  • 1,154
  • 3
  • 14
  • 31
  • 1
    this code is wrong, because `onResume()` called in first time and current activity never be running, you must handle that with one flag that set to `false` in `onCreate` and change that to `true` in `onPause` and in onResume chekc `if(flag)` start `TargetActivity` – Shayan Pourvatan Jan 10 '15 at 10:19
  • It will always start new activity. I want to start an activity when user launching app from recent's only. – Selvakumar Jan 10 '15 at 10:22