Android 开发入门-活动的最佳实践

2.6.1 知晓当前是在哪一个活动

右击 app/src/main/java/com/example/activitytest -> New -> Java Class -> BaseActivity

# app/src/main/java/com/example/activitytest/BaseActivity.java

public class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 获取当前实例的类名
        Log.d("BaseActivity", getClass().getSimpleName());
    }
}

修改活动类的继承关系,继承于 BaseActivity

public class FirstActivity extends BaseActivity {}
public class SecondActivity extends BaseActivity {}
public class ThirdActivity extends BaseActivity {}

2.6.2 随时随地退出程序

# app/src/main/java/com/example/activitytest/ActivityCollector.java

// 活动管理器类
public class ActivityCollector {

    // 存放活动的 List
    public static List activities = new ArrayList<>();

    // 添加活动的方法
    public static void addActivity(Activity activity) {
        activities.add(activity);
    }

    // 移除活动的方法
    public static void removeActivity(Activity activity) {
        activities.remove(activity);
    }

    // 销毁全部活动
    public static void finishAll() {
        for (Activity activity : activities) {
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
    }

}
# app/src/main/java/com/example/activitytest/BaseActivity.java

public class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("BaseActivity", getClass().getSimpleName());
        // 将活动放入 List 中
        ActivityCollector.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 从 List 中移除活动
        ActivityCollector.removeActivity(this);
    }

}
# app/src/main/java/com/example/activitytest/ThirdActivity.java

public class ThirdActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("ThirdActivity", "Task id is " + getTaskId());
        setContentView(R.layout.third_layout);
        Button button3 = (Button) findViewById(R.id.button_3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityCollector.finishAll();
                // 结束当前进程,保证程序完全退出
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        });

    }
}

2.6.3 启动活动的最佳写法

# app/src/main/java/com/example/activitytest/SecondActivity.java

public class SecondActivity extends BaseActivity {

    // 对外提供启动活动的方法,并定义启动参数
    public static void actionStart(Context context, String data1, String data2) {
        Intent intent = new Intent(context, SecondActivity.class);
        intent.putExtra("param1", data1);
        intent.putExtra("param2", data2);
        context.startActivity(intent);
    }
}
button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // 启动 SecondActivity 活动
        SecondActivity.actionStart(FirstActivity.this, "data1", "data2");
    }
});

你可能感兴趣的:(android)