我有三个活动LoadingPage
- > ClassActivity
- > ClassActivityEdit
。 LoadingPage
的代码是
public class LoadingPage extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loading_page);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent mainIntent = new Intent(LoadingPage.this, ClassActivity.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
LoadingPage.this.finish();
startActivity(mainIntent);
//finish();
}
}, 2000);
}
}
当我执行按钮时应用程序打开ClassActivity
视图后,单击它将移至ClassActivityEdit
。用于启动下一个活动的类活动中的代码是
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.action_settings) {
Intent addNewClass =
new Intent(ClassActivity.this, ClassActivityEdit.class);
startActivityForResult(addNewClass,1); // start the Activity
}
return super.onOptionsItemSelected(item); // call super's method
}
在ClassActivityEdit
保存活动时,我使用以下代码返回ClassActivity
View.OnClickListener saveClassActivity = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(className.getSelectedItem().toString()!="" && courseID.getSelectedItem().toString()!="" &&
activityType.getSelectedItem().toString().compareToIgnoreCase("select") != 0 && activityName.getText().length() != 0 &&
dueDate.getText().length() != 0 ){
System.out.println(activityType.getSelectedItem().toString());
AsyncTask<Object, Object, Object> saveContactTask =
new AsyncTask<Object, Object, Object>() {
@Override
protected Object doInBackground(Object... params) {
saveClassActivities(); // save classes to the database
return null;
} // end method doInBackground
@Override
protected void onPostExecute(Object result) {
Toast.makeText(ClassActivityEdit.this, activityName.getText().toString() + " saved",
Toast.LENGTH_SHORT).show();
Intent returnIntent = new Intent();
returnIntent.putExtra("result","fine");
setResult(RESULT_OK,returnIntent);
finish();
//NavUtils.navigateUpFromSameTask(ClassActivityEdit.this);
} // end method onPostExecute
}; // end AsyncTask
// save the contact to the database using a separate thread
saveContactTask.execute((Object[]) null);
}
else{
AlertDialog.Builder builder =
new AlertDialog.Builder(ClassActivityEdit.this);
builder.setTitle(R.string.errorTitle);
builder.setMessage(R.string.errorMessage);
builder.setPositiveButton(R.string.errorButton, null);
builder.show(); // display the Dialog
}
}
};
我使用
在ClassActivity
中捕获returnIntent
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
finish();
startActivity(getIntent());
}
}
}
保存数据,控制重新进入RESULT_OK状态。但该应用程序关闭时没有任何排序错误,而是留在ClassActivity
。我对上述活动的表现是
<activity
android:name=".LoadingPage"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ClassActivity"
android:label="Class Activity">
<intent-filter>
<action android:name="com.teamtreehouse.oslist.ClassActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".ClassActivityEdit"
android:label="Class Activity Edit">
<intent-filter>
<action android:name="com.teamtreehouse.oslist.ClassActivityEdit" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
感谢任何帮助
答案 0 :(得分:0)
我找到了解决方案。我正在扩展一个创造这个问题的活动。我不确定为什么,但删除对该活动的引用并在我当前的活动中实现这些方法解决了我的问题