我是android新手,在开发中遇到问题。首先我的应用程序从一个屏幕(第一个屏幕)开始。如何更改初始屏幕?
然后它会移动到另一个屏幕(第二个屏幕)。我的要求是:当我重新启动我的应用程序时,它应该从第二个屏幕开始。
如果有人知道如何处理这个问题,请告诉我。
我是android新手,在开发中遇到问题。首先我的应用程序从一个屏幕(第一个屏幕)开始。如何更改初始屏幕?
然后它会移动到另一个屏幕(第二个屏幕)。我的要求是:当我重新启动我的应用程序时,它应该从第二个屏幕开始。
如果有人知道如何处理这个问题,请告诉我。
您需要将标志存储在第一个屏幕出现的地方。我认为做这件事最好的地方是在你的应用程序共享首选项中。 (请参阅指南主题Data Storage)
然后您需要使用该标志。这是一种方法。将第二个屏幕设为默认启动活动。在onCreate方法中,检查标志是否已设置,如果没有,则使用请求代码> = 0调用startActivityForResult
作为第一个屏幕。不管标志的值如何,通常为第二个活动完成onCreate
。
您的第一项活动是在其onCreate
方法中设置成功的结果代码。然后,返回第二个活动,覆盖onActivityResult
,以便当它收到请求代码的成功结果时,它将设置标志。
这是有效的,因为如果您从第二个活动中调用startActivityForResult
,屏幕将显示第一个活动,而不是第一个闪烁第二个活动。另外,如果第一个活动由于某种原因而崩溃,第二个活动将不会收到成功结果代码,并且该标志将不会被设置,因此用户仍然会看到第一个活动。
下面是一些示例代码:
第一项活动:
private static final String PREFS_NAME = "prefs";
private static final String KEY_SHOW = "show1";
private static final int REQUEST_1 = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getBoolean(KEY_SHOW, true)) {
Intent intent = new Intent(this, Activity1.class);
startActivityForResult(intent, REQUEST_1);
}
. . . // the rest of onCreate as normal
}
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_1 && resultCode == RESULT_SUCCESS) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(KEY_SHOW, true);
editor.commit();
}
}
次活动:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setResult(RESULT_SUCCESS);
. . .
}
您好基兰我想你不希望在开始你的第一个画面每次。因此,您必须在第一次显示第一个屏幕时设置SharedPreferences中的值。那么每次你必须检查sharedPreferences中是否有任何值时?如果有任何值,则开始第二屏幕otherzise第一屏幕。
我试过这个,它工作。
编辑: -
我用这个在我的应用程序,我的要求是我不得不打开注册屏幕只有一次/只第一次。然后在启动画面我写的,
package com.z.z.z;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SplashActivity extends Activity
{
RelativeLayout lerSplash;
SharedPreferences sharedPreferences;
private String strUName_pref;
private String TAG = "SplashActivity";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Log.d(TAG, "in onCreate");
lerSplash = (RelativeLayout)findViewById(R.id.lerSplash);
Handler handler = new Handler();
handler.removeCallbacks(runnable);
handler.postDelayed(runnable, 3000L);
sharedPreferences = this.getSharedPreferences("PREFERENCE_NAME",MODE_WORLD_READABLE);
}
Runnable runnable = new Runnable()
{
public void run()
{
strUName_pref = sharedPreferences.getString("PREFERENCE_NAME", null);
if(strUName_pref != null)
{
Intent intent = new Intent(SplashActivity.this,MainTabActivity.class);
startActivity(intent);
finish();
}
else
{
Intent intent = new Intent(SplashActivity.this,RegistrationActivity.class);
startActivity(intent);
finish();
}
}
};
}
然后在RegistrationActivity我写
package com.z.z.z;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class RegistrationActivity extends Activity
{
SharedPreferences sharedPreferences;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.registration);
Log.d(TAG, "in onCreate");
sharedPreferences = this.getSharedPreferences("PREFERENCE_NAME",MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = sharedPrefs.edit();
prefsEditor.putString("SharedPreferenceValue", "YOUR_VALUE_WHICH_YOU_WANT_TO_PUT");
prefsEditor.commit();
}
}
你想要什么到底是什么?
如果您希望每次从应用程序退出时启动应用程序,则必须在SharedPreference中存储值并在每个屏幕中对其进行升级,并在Restart/run上检查该值并启动该活动。
否则您还有另一个选项,由我的朋友张贴上面...尝试一个。
在清单中设置默认活动后,无法更改默认活动。你可以做的是在应用程序的开始处有一个默认的活动(可能像一个启动画面)。 在活动1在默认的活动有此代码现在
private SharedPreferences appPreferences = getSharedPreferences("user", MODE_PRIVATE);
final SharedPreferences.Editor editor = appPreferences.edit();
editor.putBoolean("flag", true);
editor.commit();
if(editor.getBoolean("flag",false))
{
startActivity(new Intent(DefaultActivity.this,Activity1.class));
}
else
{
startActivity(new Intent(DefaultActivity.this,Activity2.class));
}
使这个布尔值false
使用份额的偏好。 1.从共享的pref中获取boolean var,并检查它是true还是false。 2.如果它是假的,则显示第一个屏幕,将变量设置为true并将变量保存在共享首选项中。否则显示第二个屏幕
我有这个相同的问题。我对Android开发非常陌生 - 所以我的答案可能有缺陷,但这对我来说很有用。
转到您的AndroidManafest.xml文件。您的活动定义部分应该是这个样子:
...
<activity
android:name="com.example.test.OriginalFirstScreen"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.test.NewFirstScreen"
android:label="@string/title_activity_new_first_screen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
...
我删除从OldFirstScreen活动定义标签和左/中NewFristScreen定义添加它。这个伎俩。见下 -
...
<activity
android:name="com.example.test.OriginalFirstScreen"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity
android:name="com.example.test.NewFirstScreen"
android:label="@string/title_activity_new_first_screen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
...
重新启动的条件是什么? – 2012-01-13 05:37:15
再次运行我的应用程序 – kiran 2012-01-13 05:39:38