[英]Can I use shared preferences to create login sessions in android studio when I have two types of activities to intent depending on the type of user?
[英]Can I launch different Activities on startup depending on user preferences?
我有一個ListActivity和一個MapActivity。 我想在用戶在首選項窗口中選擇的應用程序啟動時啟動其中一個活動。
到目前為止,我看到在應用程序啟動時啟動活動的唯一方法是使用...在應用程序清單文件中指定它。
<activity android:name=".MyActiivty"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
我想我可能不得不開始一項活動,除了查看用戶首選項之外什么都不做,然后啟動ListActivity或MapActivity。 看起來像浪費一樣,活動什么都不做,只能啟動另一項活動。 在我的研究中,我沒有找到解決這個問題的方法。 任何建議將不勝感激。
謝謝和問候,戴夫
首先,不要創建第三個活動。 只需將LAUNCHER
Activity
作為列表或映射,並在需要時調用setContentView()
之前在onCreate()
調用另一個(加上finish()
)的startActivity()
。 這樣,大約50%的時間,你正在開展正確的活動。
原則上,您可以讓兩個活動都具有LAUNCHER
<intent-filter>
,僅啟用一個。 但是,這不適用於桌面快捷方式,這些快捷方式將路由到特定活動(在制作快捷方式時恰好配置的那個)。 如果這不關心你,你可能會走這條路。 但是,嘗試使用一些設備和自定義主屏幕進行測試 - 我不確定是否每個人都會立即接受您的更改。
我剛剛將以下代碼添加到onCreate()方法中,它就像魅力一樣。
Intent intent;
intent = new Intent(this, MyMap.class);
startActivity( intent );
finish();
對於新朋友(我),以下是戴夫的回答,以及我需要對AndroidManifest.xml進行的更改。
主要活動:
Intent intent;
intent = new Intent(this, DisplayMessageActivity.class);
startActivity( intent );
更改為xml文件,來自 - > http://developer.android.com/training/basics/firstapp/starting-activity.html
AndroidManifest.xml中:
<activity
android:name="com.mycompany.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.mycompany.myfirstapp.MyActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mycompany.myfirstapp.MyActivity" />
</activity>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.