// Path: app/src/main/java/com/example/ddmeng/helloactivityandfragment/utils/TaskUtils.java
// public class TaskUtils {
// 
//     private static final String LOG_TAG = "Stack";
// 
//     public static String getCurrentTopActivityName(Context context) {
// 
//         ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
// 
//         //this method requires android.permission.GET_TASKS
//         List<ActivityManager.RunningTaskInfo> runningTasks = activityManager.getRunningTasks(2);
//         //this method was deprecated in API level 21
//         ComponentName topActivity = runningTasks.get(0).topActivity;
//         Log.i(LOG_TAG, "top Activity: " + topActivity.getShortClassName());
//         return topActivity.getShortClassName();
// 
//     }
// }


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.example.ddmeng.helloactivityandfragment.R;
import com.example.ddmeng.helloactivityandfragment.utils.TaskUtils;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
package com.example.ddmeng.helloactivityandfragment.launchmode;




public class SingleTopActivity extends Activity {

    private static final String LOG_TAG = "Launch Mode";
    @BindView(R.id.current_task)
    TextView mTaskText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.launch_mode_single_top_activity);
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

        Log.i(LOG_TAG, "Single Top Activity, onCreate(), " + this.hashCode());
        Log.i(LOG_TAG, "task id: " + this.getTaskId());
        mTaskText.setText("Currrent Task: " + this.getTaskId());

        //strange phenomenon:
        // When start this activity, startActivity() is called twice deliberately
        // then the onCreate() is called once (right).
        // But when press back, the onCreate() is called again (strange)
        // So using normal eyes, the singleTop activity appear two times.

        //Conclusions: Do NOT call startActivity() twice
        // Because before onCreate() invoked, the singleTop activity are still not considered as on the top of the stack

// Path: app/src/main/java/com/example/ddmeng/helloactivityandfragment/utils/TaskUtils.java
// public class TaskUtils {
// 
//     private static final String LOG_TAG = "Stack";
// 
//     public static String getCurrentTopActivityName(Context context) {
// 
//         ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
// 
//         //this method requires android.permission.GET_TASKS
//         List<ActivityManager.RunningTaskInfo> runningTasks = activityManager.getRunningTasks(2);
//         //this method was deprecated in API level 21
//         ComponentName topActivity = runningTasks.get(0).topActivity;
//         Log.i(LOG_TAG, "top Activity: " + topActivity.getShortClassName());
//         return topActivity.getShortClassName();
// 
//     }
// }


// Path: app/src/main/java/com/example/ddmeng/helloactivityandfragment/launchmode/SingleTopActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.example.ddmeng.helloactivityandfragment.R;
import com.example.ddmeng.helloactivityandfragment.utils.TaskUtils;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

package com.example.ddmeng.helloactivityandfragment.launchmode;




public class SingleTopActivity extends Activity {

    private static final String LOG_TAG = "Launch Mode";
    @BindView(R.id.current_task)
    TextView mTaskText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.launch_mode_single_top_activity);
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

        Log.i(LOG_TAG, "Single Top Activity, onCreate(), " + this.hashCode());
        Log.i(LOG_TAG, "task id: " + this.getTaskId());
        mTaskText.setText("Currrent Task: " + this.getTaskId());

        //strange phenomenon:
        // When start this activity, startActivity() is called twice deliberately
        // then the onCreate() is called once (right).
        // But when press back, the onCreate() is called again (strange)
        // So using normal eyes, the singleTop activity appear two times.

        //Conclusions: Do NOT call startActivity() twice
        // Because before onCreate() invoked, the singleTop activity are still not considered as on the top of the stack

        TaskUtils.getCurrentTopActivityName(this);