我有这个活动:
public class AMLoginActivity extends Activity implements IAsyncResponse {
public static int finished;
private final String TAG = "AMLoginActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
finished = 0;
Intent i = new Intent(AMLoginActivity.this, GDLoginActivity.class);
i.putExtra("response", this);
AMLoginActivity.this.startActivity(i);
}
public void processFinish(){
finished++;
Log.i(TAG, "Number finished: " + finished);
if(finished == 1){
Log.i(TAG, "All finished");
AMLoginActivity.this.finish();
Log.i(TAG, "Finish called");
}
}
调用GDLoginActivity登录谷歌硬盘。可以在下面看到:
public class GDLoginActivity extends Activity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = "GDLoginActivity";
private static final int REQUEST_CODE_RESOLUTION = 3;
private static GoogleApiClient mGoogleApiClient;
@Override
protected void onResume() {
super.onResume();
if (mGoogleApiClient == null) {
// Create the API client and bind it to an instance variable.
// We use this instance as the callback for connection and connection
// failures.
// Since no account name is passed, the user is prompted to choose.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
Log.i(TAG, "New GDClient created");
}
// Connect the client.
mGoogleApiClient.connect();
}
@Override
protected void onPause() {
if (mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
}
super.onPause();
}
@Override
public void onConnected(Bundle connectionHint) {
Log.i(TAG, "API client connected.");
IAsyncResponse response = (IAsyncResponse) getIntent().getSerializableExtra("response");
Log.i(TAG, "Process finish");
response.processFinish();
finish();
}
@Override
public void onConnectionSuspended(int cause) {
Log.i(TAG, "GoogleApiClient connection suspended");
}
@Override
public void onConnectionFailed(ConnectionResult result) {
// Called whenever the API client fails to connect.
Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
if (!result.hasResolution()) {
// show the localized error dialog.
GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show();
return;
}
// The failure has a resolution. Resolve it.
// Called typically when the app is not yet authorized, and an
// authorization
// dialog is displayed to the user.
try {
result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
} catch (IntentSender.SendIntentException e) {
Log.e(TAG, "Exception while starting resolution activity", e);
}
}
public static GoogleApiClient getGoogleApiClient(){
return mGoogleApiClient;
}
}
问题是当GDLoginActivity连接并结束()时,它应该增加1并且还完成AMLoginActivity。它增加1并在processFinish()中调用finish(),但没有任何反应,AMLoginActivity实际上没有关闭(即onDestroy()永远不会被调用),所以我只剩下一个空白屏幕。如果我删除GDLoginActivity而只是调用processFinish(),那么AMLoginActivity就可以了,所以我认为它与GDLoginActivity有关,但这也与其他类似的活动有关。有任何想法吗?
编辑:此外,如果我点击空白屏幕上的后退按钮,它会调用AMLoginActivity的onDestroy()方法并进入我想要的活动,如果这暗示了正在发生的事情的线索?
答案 0 :(得分:0)
看起来你正在以奇怪的顺序完成它们。在完成启动它之前尝试完成可见活动。
@Override
public void onConnected(Bundle connectionHint) {
Log.i(TAG, "API client connected.");
IAsyncResponse response = (IAsyncResponse) getIntent().getSerializableExtra("response");
Log.i(TAG, "Process finish");
//finishes the previous activity
response.processFinish();
//finishes the visible activity
finish();
//try flipping this order ^
}
如果您尝试完成另一项活动,则可以使用startActivitForResult()
。
答案 1 :(得分:0)
您需要先使用intent
context
将当前startActivity
发送到另一项活动。之后,您可以从重定向的位置完成当前活动。
$(document).ready(function(){
if($('#stname').val() == ""){
$('#update').hide();
$('#newstore').show();
}
else {
$('#update').show();
$('#newstore').hide();
}
});
您可以使用一些方法,例如:
从活动A开始活动B
Intent intent = new Intent(this, FirstActivity.Class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish(); // Call once you redirect to another activity
如果用户点击修改按钮,请使用FLAG_ACTIVITY_CLEAR_TOP启动活动A.还要在额外标记中传递标记。
startActivity(new Intent(A.this, B.class));
如果用户点击修改按钮,请使用FLAG_ACTIVITY_CLEAR_TOP启动活动A.还要在额外标记中传递标记。
Intent i = new Intent(B.this, A.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("flag", "modify");
startActivity(i);
finish();
现在使用活动A的onCreate()方法,写下代码。
Intent i = new Intent(B.this, A.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("flag", "add");
startActivity(i);
finish();
这是与切换活动相关的SO票证: android how to finish an activity from other activity