Android app中默认启动的activity
#
在一个app中,可以有多个activity ,每个activity都是同等级的。我们需要设置那些要显示在launcher中
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
通过 action.main 与category.LAUNCHER 配合使用,可以设置activity 在launcher显示。
默认启动项,满足上述的 intent filter的activity 就可以作为默认启动页面。
当AndroidMainfest.xml 中存在多个activity,且多个activity的 intent filter 属性都满足 action.main,category.launcher 时,
默认启动的activity 为 最上面的activity。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mylauncher">
<application
android:allowBackup="true"
android:name=".MyApplication"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" android:label="activity1">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity2" android:label="activyt2">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
public class MainActivity extends Activity implements View.OnClickListener {
private EditText editText;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("onCreate",this.getClass().getName());
editText = (EditText) findViewById(R.id.edit);
btn = (Button) findViewById(R.id.save);
btn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.save){
String data = editText.getText().toString();
if (null!=data){
MyApplication application = (MyApplication) getApplication();
application.setData(data);
} else {
Toast.makeText(this,"data is null!",Toast.LENGTH_LONG).show();
}
}
}
}
public class MainActivity2 extends Activity implements View.OnClickListener {
private EditText editText;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("onCreate",this.getClass().getName());
editText = (EditText) findViewById(R.id.edit);
btn = (Button) findViewById(R.id.save);
btn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.save){
String data = editText.getText().toString();
if (null!=data){
MyApplication application = (MyApplication) getApplication();
application.setData(data);
} else {
Toast.makeText(this,"data is null!",Toast.LENGTH_LONG).show();
}
}
}
}
运行结果
01-06 15:54:12.019 25400-25400/com.mylauncher D/onCreate: com.mylauncher.MainActivity