[英]startActivity() doesn't start activity
I have a class called ShowBoardList where I check if user has logged in. If user hasn't logged in, then I want to return to the MainActivity which provides the user with buttons to login into different services. 我有一个名为ShowBoardList的类,在其中检查用户是否已登录。如果用户尚未登录,那么我想返回MainActivity ,它为用户提供了用于登录不同服务的按钮。
My AndroidManifests.xml looks like this: 我的AndroidManifests.xml如下所示:
<application
<activity android:name="im.chaitanya.TaskTimer.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="im.chaitanya.TaskTimer.WebViewActivity" >
</activity>
<activity android:name="im.chaitanya.TaskTimer.ShowBoardList"
android:label="Your Tasks">
</activity>
</application>
ShowBoardList.java looks like this: ShowBoardList.java看起来像这样:
...
Intent mainActivityIntent = new Intent(ShowBoardList.this, im.chaitanya.TaskTimer.MainActivity.class);
Intent intent = getIntent();
String url = intent.getStringExtra(WebViewActivity.EXTRA_MESSAGE); //url can be null here
Keys keys = new Keys(); //this gets an API key
SharedPreferences settings = getSharedPreferences("mySettings", 0);
String savedToken = settings.getString("token", "Empty");
if (MyUtils.equalsWithNulls(url,"tasktimer://oauthresponse#token=")) {
Log.d("From ME:", "I've reached inside url check");
mainActivityIntent.putExtra(caller, "ShowBoardList");
//caller is a String. I'm storing the name of the current activity (ShowBoardList) in it.
//So that the main activity (which I'm trying to call) will know where the call came from.
startActivity(mainActivityIntent);
}
if(savedToken.equals("Empty") || savedToken.equals("")) {
String searchString = "#token=";
int tokenIndex = url.indexOf(searchString) + searchString.length(); //Since url can be null there can be an error here
String token = url.substring(tokenIndex);
savedToken = token;
SharedPreferences.Editor editor = settings.edit();
editor.putString("token", token);
editor.apply();
}
...
Condtion equalsWithNulls checks if url is null OR equal to the string in the argument. 条件equalsWithNulls检查url是否为null或等于参数中的字符串。 I have log statements there to check whether control reaches inside the if statement.
我在那里有日志语句,以检查控件是否到达if语句内部。 The main activity however doesn't start.
但是,主要活动并未开始。
Edit: onCreate() of MainActivity.java looks like this: 编辑: MainActivity.java的 onCreate()看起来像这样:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences settings = getSharedPreferences("mySettings", 0);
String token = settings.getString("token", "Empty");
Intent intent = new Intent(this, ShowBoardList.class);
if(token != "Empty") {
startActivity(intent);
}
intent = getIntent();
String callerActivity = intent.getStringExtra(ShowBoardList.caller);
View coordinatorLayoutView = findViewById(R.id.snackbarPosition);
if (callerActivity!=null && callerActivity == "ShowBoardList") {
Snackbar
.make(coordinatorLayoutView, "Permission Denied", Snackbar.LENGTH_LONG)
.show();
}
setContentView(R.layout.activity_main);
}
Try to define your new Intent wherever you required. 尝试在任何需要的地方定义新的Intent。
Intent newIntent = new Intent(ShowBoardList.this, im.chaitanya.TaskTimer.MainActivity.class);
newIntent .putExtra(caller, "ShowBoardList");
startActivity(newIntent );
My solution is based on Sourabh's comment on the question. 我的解决方案基于Sourabh对问题的评论。 I realised from my logs that the activity was indeed being started.
我从日志中意识到该活动确实已经开始。
What I didn't realise was that when startActivity()
is called, the calling activity (in this case ShowBoardList
) is paused and when ShowBoardList
was being called again, it would resume from after startActivity()
. 我没有意识到的是,当调用
startActivity()
时,调用活动(在本例中为ShowBoardList
)被暂停,而在再次调用ShowBoardList
时,它将从startActivity()
之后恢复。
Therefore the solution here was to call finish()
and then return
immediately after the startActivity() which ensures that onCreate is called the next time. 因此,这里的解决方案是调用
finish()
,然后在startActivity()之后立即return
,以确保下次调用onCreate。 I hope that makes sense if anyone is in the same situation. 如果有人处于相同的情况,我希望这是有道理的。
These questions helped me understand more about finish()
: 这些问题帮助我进一步了解了
finish()
:
about finish() in android 关于android中的finish()
onCreate flow continues after finish() finish()之后,onCreate流继续
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.