我的SplashActivity
是这样的:
public class SplashActivity extends Activity {
Handler Handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Handler = new Handler();
Handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}, 1500);
Intent appLinkIntent = getIntent();
String appLinkAction = appLinkIntent.getAction();
Uri appLinkData = appLinkIntent.getData();
}
}
其在AndroidManifext.xml
中的声明如下:
<activity
android:name=".SplashActivity"
android:theme="@style/Splash"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts"/>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="brokenhearts.ml"
android:scheme="http"
android:pathPattern="/*"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="brokenhearts.ml"
android:scheme="https"
android:pathPattern="/*"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="www.brokenhearts.ml"
android:scheme="http"
android:pathPattern="/*"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="www.brokenhearts.ml"
android:scheme="https"
android:pathPattern="/*"/>
</intent-filter>
</activity>
我在我的应用程序中使用WebViews。问题是,每当用户在应用程序的其他活动中点击我的网站的链接时,它便从SplashActivity
重新开始。这在某种程度上是正确的,因为那是我添加意图的地方。但是,我想知道此设置是否还有其他方法仅在从启动器或从URL意图启动(当应用程序未运行时)而不是从URL意图启动时显示启动屏幕。当应用程序已经在前台运行时被触发。
如果无法通过这种设置进行设置,我还有什么其他方法可以解决?
答案 0 :(得分:2)
检查appLinkData是否为null,然后显示SplashScreen,否则启动MainScreen
public class SplashActivity extends Activity
{
Handler Handler;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Intent appLinkIntent = getIntent();
String appLinkAction = appLinkIntent.getAction();
Uri appLinkData;
if(appLinkAction!=null)
appLinkData = appLinkIntent.getData();
if(appLinkData!=null)
{
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
finish();
return;
}else
{
Handler = new Handler();
Handler.postDelayed(new Runnable()
{
@Override
public void run()
{
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
},
1500);
}
}
}
答案 1 :(得分:2)
在您的情况下,您可以考虑覆盖Android的onNewIntent
生命周期功能。这将处理此SplashActivity
的所有先前创建,并带有新的意图。这是一个示例:
在应用程序的其他活动中,再次启动SplashActivity
时,您可能需要这样启动活动。
public class OtherActivityContainingWebView extends Activity {
public void onCreate(Bundle savedInstanceState) {
Intent i = new Intent(this, SplashActivity.class);
i.putExtra("url", "www.your-website-or-any-other-data-to-be-passed-to-splash-activity");
startActivity(i);
}
}
您需要像这样在onNewIntent
中拥有SplashActivity
函数。
public class SplashActivity extends Activity {
public void onCreate(Bundle SavedInstanceState) {
// Your current code goes here ...
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String url = intent.getStringExtra("url");
// Do something with the value passed from the other activity
// Then launch the Main Activity from here.
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}
希望有帮助!