静的変数を持つパブリック クラス名 Constants を使用しています。
/*
 * Flags
 */
public static boolean gotCourse = false;
public static boolean quizTaken = false;
1 番目のアクティビティでこれらのフラグを変更し、2 番目に移動します (1 番目のアクティビティも終了します) が、2 番目のアクティビティでは、変更された値ではなくフラグの初期値を取得しています。なぜこの問題が発生するのですか?? アプリの静的変数に制限はありますか?
また、ロジックが複雑になるため、これらの値を意図を介して渡したくありません。
最初のアクティビティのコード:
@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;
2 番目の活動のコード:
@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();
        }
    }
}
ProgramPlay 機能:
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);
    }
編集:もう1つ。1 番目のアクティビティを終了せずに 2 番目のアクティビティを開始すると、2 番目のアクティビティで値が変更されます。
更新:でアクティビティを終了すると問題が発生しますが、 で終了すると問題は発生しSystem.exit(0);ませんfinish();。しかし、それでアクティビティを終了するfinish();と、より多くの実行時エラーが発生します。