![](/img/trans.png)
[英]Why does isFinishing() behaves differently in entry point Activity (with android.intent.action.MAIN) and non entry point Activity
[英]When I remove the <action android:name=“android.intent.action.MAIN” /> from the manifest the the application starts from the 2nd activity
當我添加
<action android:name="android.intent.action.MAIN" />
在清單文件中,應用程序從第二個活動開始。 如果我不采取行動,
<action android:name="android.intent.action.MAIN" />
則應用程序無法啟動。 如果我刪除
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Gradle提供了一個錯誤。 “運行Main2Activity時出錯:必須將活動導出或包含一個意圖過濾器”
這是清單文件的代碼
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dell_7560.experiment3">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main2Activity"
android:label="@string/title_activity_main2">
<!--if I remove this, I get an error mentioned above-->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
活動1文件
package com.example.dell_7560.experiment3;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
LinearLayout ll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll=(LinearLayout)findViewById(R.id.ll1);
ll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new Intent(MainActivity.this,Main2Activity.class);
startActivity(i);
}
});
}
}
活動2文件
package com.example.dell_7560.experiment3;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
poupulateListView();
}
public void poupulateListView() {
//get the values from res
Resources res=getResources();
String data_items[]=res.getStringArray(R.array.details);
//build a adapter
ArrayAdapter<String> adapter=new ArrayAdapter<String>
(this,
android.R.layout.simple_list_item_1,//using a default layout
data_items)//elements fed to the adapter
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view=super.getView(position,convertView,parent);
TextView tv=(TextView)view.findViewById(android.R.id.text1);
tv.setTextColor(getResources().getColor(R.color.occurYellow));
return view;
}
};
//why do we android before R? cause normally we use R.id.something
//configure the adapter
ListView lv=(ListView) findViewById(R.id.list_view_1);
lv.setAdapter(adapter);
}
}
意圖過濾器:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
在清單中告訴啟動器,這是要啟動的主要活動。 這兩個事物必須存在,因此被認為是要啟動的活動。
如果您有2個使用此Intent過濾器的活動,並且從第一個活動中刪除了一個參數,則不再識別該參數,並且將使用下一個匹配項。 在這種情況下,您的第二項活動。
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
這表示該活動是打開應用程序時android必須啟動的活動。 您應該對此進行一項活動。
清單文件僅在以下行之間的應用程序標記之間添加。
<activity android:name=".Main2Activity"
android:label="@string/title_activity_main2"/>
在清單文件中,只有第一個調用應用程序的活動啟動了該活動,僅添加了意圖過濾器,然后才添加了上面的代碼。
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.