对某些Android清单和活动协调有疑问。我创建了一个直接进入主要活动的应用程序,用户可以从该应用程序与应用程序进行交互。我想创建一个活动,显示应用程序的简单标题(可能带有图片,但后来......),然后在几秒延迟后切换到主要活动。这是我到目前为止所拥有的......
TitleActivity:
public class TitleActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_title);
try {
Thread.sleep(3000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
}
...
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="khandy.application.convertible"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="khandy.application.convertible.TitleActivity"
android:label="@string/title_activity_title" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="khandy.application.convertible.MainActivity"
android:label="@string/app_name" >
</activity>
...
启动时发生的事情是我的视图/小部件都没有显示在title_activity上,而3秒延迟发生,然后它切换到主要活动。然后,当我按下android后退按钮时,我退回到标题活动,该活动与视图/小部件一起运行。我很确定没有出现的视图/小部件与我非常粗略的多线程有关...
所以,我的问题是:
谢谢!
答案 0 :(得分:1)
我想要一个SplashScreen:
public class TitleActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_title);
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
Intent intent = new Intent(TitleActivity.this,MainActivity.class);
TitleActivity.this.startActivity(intent);
TitleActivity.this.finish();
}
}, 3000);
}
}
答案 1 :(得分:1)
在mainactivity启动后完成标题活动,这解决了问题2.使用
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
finish();
问题1,使用以下代码
public class SplashScreen extends Activity{
private SplashHandler mSplashHandler = new SplashHandler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
mSplashHandler.sendEmptyMessageDelayed(SplashHandler.TIMER_EXPIRED, 5);
}
class SplashHandler extends Handler {
static final int TIMER_EXPIRED = 100;
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case TIMER_EXPIRED:
Intent in = new Intent(SplashScreen.this, nextActivity.class);
startActivity(in);
finish();
break;
default:
break;
}
}
}
@Override
protected void onDestroy() {
mSplashHandler = null;
super.onDestroy();
}
}