Java startActivity()不';不要开始活动
我有一个名为ShowBoardList的类,我在其中检查用户是否已登录。如果用户尚未登录,那么我想返回到main活动,该活动为用户提供了登录不同服务的按钮 我的AndroidManifests.xml如下所示:Java startActivity()不';不要开始活动,java,android,Java,Android,我有一个名为ShowBoardList的类,我在其中检查用户是否已登录。如果用户尚未登录,那么我想返回到main活动,该活动为用户提供了登录不同服务的按钮 我的AndroidManifests.xml如下所示: <application <activity android:name="im.chaitanya.TaskTimer.MainActivity" > <intent-filter> <action and
<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>
...
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();
}
...
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);
}
conditionequalsWithNulls检查url是null还是等于参数中的字符串。我在那里有日志语句来检查控件是否到达if语句内部。然而,主要活动并未开始
MainActivity.java的编辑:onCreate()如下所示:
<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>
...
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();
}
...
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);
}
在需要的地方尝试定义你的新意图
Intent newIntent = new Intent(ShowBoardList.this, im.chaitanya.TaskTimer.MainActivity.class);
newIntent .putExtra(caller, "ShowBoardList");
startActivity(newIntent );
我的解决方案是基于苏拉布对这个问题的评论。我从日志中意识到,活动确实已经开始了 我没有意识到的是,当调用
startActivity()
时,调用活动(在本例中是ShowBoardList
)会暂停,当再次调用ShowBoardList
时,它会从startActivity()
之后恢复
因此,这里的解决方案是调用finish()
,然后在startActivity()之后立即返回以确保下次调用onCreate。我希望,如果有人处于同样的情况,这是有道理的
这些问题帮助我进一步了解了finish()
:
有什么错误吗?怎么搞的?我用调试器来检查发生了什么。控件遍历该语句并转到if语句外的下一行。那边没有错。后来出现了一个错误,但这是因为主活动没有启动,程序试图调用空对象上的函数。我添加了一些代码。这应该让我更清楚地知道我在努力完成什么。但是就像我说的,startActivity()语句没有错误。startActivity()
不是同步的。它立即返回,活动稍后开始。这是你的问题吗?可能是。如何等待活动开始?