我正在学习开发Android应用并且目前正在使用Android Studio。我已经创建了两个测试程序的活动:启动画面和菜单/家庭活动。我对布局设计做得很好,但我遇到了一些编程问题:当我按下" Back"在我的模拟器上,我的家庭活动没有完成并退出应用程序。只有在第二次按下之后我才设法退出。我的闪屏很好,但家庭活动的情况并非如此。看起来活动重新启动(即使启动它我使用了明确的意图。我尝试添加android:noHistory =" true"到清单。我尝试了一些onbackpressed,我在网上找到的doubleBackToExitPressedOnce代码,但没有好运。我不太确定默认设置是什么,但似乎没什么用。我不知道我是否跳过某些东西。我希望你能帮助我。谢谢。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.apptest" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:screenOrientation="nosensor"
android:configChanges="keyboardHidden|orientation">
<activity
android:name=".SplashScreen"
android:label="@string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".HomeScreen"
android:label="@string/title_activity_home_screen"
android:noHistory="true">
</activity>
</application>
</manifest>
public class HomeScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.homescreen);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_splash_screen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
public class SplashScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.splashscreen);
ImageView flashscreenView = (ImageView) findViewById(R.id.splashscreenanimation);
flashscreenView.setBackgroundResource(R.drawable.flashscreenanimation);
AnimationDrawable flashscreenanimation = (AnimationDrawable) flashscreenView.getBackground();
flashscreenanimation.start();
Thread loading = new Thread() {
public void run() {
try {
sleep(2730);
finish();
Intent splash = new Intent(SplashScreen.this, HomeScreen.class);
startActivity(splash);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
};
loading.start();
}
}
答案 0 :(得分:0)
可能是因为模拟器尝试在移动设备中使用。
答案 1 :(得分:0)
@KaranMer帮我解决了这个问题。我有两个意图(一个在尝试,另一个在最后,我只需要摆脱一个。
答案 2 :(得分:-1)
覆盖HomeScreen活动中的onBackPressed()方法。
@Override
public void onBackPressed() {
finish();
}