我正在使用一个公共类名常量,其中有静态变量。
/*
* Flags
*/
public static boolean gotCourse = false;
public static boolean quizTaken = false;
我在第一个活动中更改这些标志并移动到第二个(我也退出第一个活动)但在第二个活动中我得到标志的初始值而不是更改的值。为什么我会遇到这个问题??应用程序的静态变量是否有任何限制?
而且我也不想通过意图传递这些值,因为它们会使我的逻辑复杂化。
第一次活动的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_moviewrapper);
Constants.quizTaken = extra.getBoolean("fromquiz", false);
Log.d(Constants.TAG,"Got Course Value: "+Constants.gotCourse);
if(Constants.gotCourse == false){
mGetDataTask = new GetDataTask();
mGetDataTask.execute();
Constants.gotCourse = true;
}else{
Log.d(Constants.TAG,"In Play");
playProgram();
}
Log.d(Constants.TAG,"Got Course Value: "+Constants.gotCourse);
}
我正在改变的地方Constants.gotCourse = true;
第二次活动代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(Constants.TAG,"Got Course Value in Video Wrapper: "+Constants.gotCourse);
}
任务代码:
private class GetDataTask extends AsyncTask<String, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(MovieWrapperActivity.this);
// can use UI thread here
protected void onPreExecute() {
this.dialog.setMessage("Loading...");
this.dialog.setCancelable(false);
this.dialog.show();
}
// automatically done on worker thread (separate from UI thread)
protected Void doInBackground(final String... args) {
getCourse();
return null;
}
// can use UI thread here
protected void onPostExecute(final Void unused) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
playProgram();
}
}
}
节目播放功能:
private void playProgram(){
Intent intent = new Intent(MovieWrapperActivity.this, VideoWrapperActivity.class);
int chap_played = extra.getInt("chapterplayed",-1);
Log.d(Constants.TAG,"Played: "+chap_played +" : chapter: "+Constants.mCourse.getTotalChapters());
if(chap_played == -1){
//Play 1st Chapter
intent.putExtra("chaptertoplay", 1);
//Chapter cha = mCourse.getmChapters().get(1);
intent.putExtra("videofile", Constants.mCourse.getmChapters().get(0).getVideoURL());
if(Constants.mCourse.getmChapters().get(0).isQuiz()){
Constants.quizTaken = false;
}else{
Constants.quizTaken = true;
}
}
else if (chap_played < Constants.mCourse.getmChapters().size()){
// For Playing 2nd to till last
if(Constants.mCourse.getmChapters().get(chap_played-1).isQuiz() && !Constants.quizTaken){
Intent in2 = new Intent(MovieWrapperActivity.this, QuestionWrapper.class);
in2.putExtra("chaptertoplay", chap_played);
Constants.quizTaken = true;
startActivity(in2);
//System.exit(0);
}else{
if(Constants.mCourse.getmChapters().get(chap_played).isQuiz()){
Constants.quizTaken = false;
}else{
Constants.quizTaken = true;
}
intent.putExtra("videofile", Constants.mCourse.getmChapters().get(chap_played).getVideoURL());
intent.putExtra("chaptertoplay", chap_played+1);
}
}
else if(chap_played == Constants.mCourse.getmChapters().size()){
// Move to End Knowlege Test Here
MovieWrapperActivity.this.finish();
}
Log.d(Constants.TAG,"Here...");
startActivity(intent);
System.exit(0);
}
编辑:还有一件事。当我没有完成第一个活动并开始第二个活动时,我在第二个活动中得到了改变。
更新:当我完成 Activity 时出现问题,System.exit(0);
但在完成时没有问题finish();
。但是当使用finish();
它完成活动时会导致更多的运行时错误。