关于android:Android-开发入门活动的最佳实践

44次阅读

共计 2453 个字符,预计需要花费 7 分钟才能阅读完成。

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<Activity> 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");
    }
});

正文完
 0