CrossWord游戏应用程序-重置所有静态变量,或者首先放置静态变量是一个坏主意

时间:2019-04-07 07:54:48

标签: android android-intent static-variables

这是应用程序的流程。

进程1
    -在游戏启动时检查SharedPref中最后播放的ID
    ----如果在本地存储负载中可用,请继续
    ----否则尝试从Internet加载
    -------如果没有互联网,则显示带有退出,加载按钮且没有取消操作的警报对话框

可选过程
    -显示日期选择器并在选择时将日期值保存到Sharedpref last-played-id
    -在重新创建应用程序时,继续执行Process-1

show structure is as follows
// most of the variables are declared as static
private static boolean crossword_finished = false;
private static boolean is_allok = false;
private static String last_date="0";
private static CrossWord crossword;
private static String[] main_GridCells;//<-- this will be set as adapter to GridView

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       // gamechanged will be set when date is selected
        if( getIntent().getBooleanExtra("gamechanged", false)){
            // Do I need to reset all static variable here or
            // is it a bad idea to have static variable in first place.

        }
        // loadcrossword object (Process-1)
        if(crossword!=null){
        is_finished= crossword.getIsFinished();
        main_GridCells = crossword.getGridCells();
        initViews();
        if(!is_finished){
           initListeners();
        }
        //finally set
        is_allok=true;
    }
    }//end of onCreate()

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && is_allok || crossWord!=null) {
            //finally to select previously selected cell in crossword
            //https://stackoverflow.com/a/27673564 & https://stackoverflow.com/a/17840491
            //DO NOT call below method in onCreate(); Because the screen is not fully visible to the user.
            selectLastCell();//perform performItemClick on gridcell
            checkCrossWordFinished();
        }
    }
    initListeners(){

        //other listeners
        mDateSetListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int month, int day) {
               // after selection restart activity;
               restartActivity();

        };
    }
    }


    private void restartActivity() {
        //https://stackoverflow.com/a/16480930/538212
        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("gamechanged", true);
        startActivity(intent);
        finish();
    }

我需要在这里重置所有静态变量还是     首先放置静态变量是一个坏主意。
    或
    android中是否有一种方法可以重新创建活动,以便所有变量(包括静态变量)都应从头开始初始化?

0 个答案:

没有答案