使用共享首选项还原应用程序背景

时间:2014-12-15 02:22:43

标签: android colors background sharedpreferences

我知道有人问过类似的问题,但我严重关注这个问题。 当我关闭应用程序时,它会显示“保存数据”,这意味着我正在调用我的on stop方法,但是当我重新打开应用程序时,它没有提到它是“获取数据”意味着我的onStart方法没有被调用。

中间的代码块似乎是最重要的,我只想包含所有内容,这样如果有人再次碰到我的情况,他们就能看到一切。

在这种情况下如何使用sharedpreferences类来保存背景颜色并在重新打开应用程序时调用onStart方法? 截至目前,它始终采用默认颜色。 感谢您的时间。

 EditText ed;

    String background_color;
    String Color_Value;
    String Message;

    int datablock =100;
    int selectedItem;
    int Color_Position;
    int change=0;

    View root;
    View someView;

    SharedPreferences prefer;
    SharedPreferences.Editor prefer_edit;


    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_check_box);
            ActionBar bar = getActionBar();
            bar.setDisplayShowTitleEnabled(false);
            bar.setDisplayShowHomeEnabled(false);
            //Hides app title and icon

            if (Color_Value!= null){
                onStart();
            }
            else {

            }

            bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
            //Creates a spinner

            ed = (EditText)findViewById(R.id.edit);

            final String[] dropdown = getResources().getStringArray(R.array.Colors);
            ArrayAdapter <String> adapter = new ArrayAdapter<String>(bar.getThemedContext(),
            android.R.layout.simple_spinner_item,android.R.id.text1,dropdown);
            //sets up the drop down navigation list in the action bar
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            bar.setListNavigationCallbacks(adapter, new OnNavigationListener(){

                @Override
                public boolean onNavigationItemSelected(int selectedItem, long arg1) {
                    //Java always starts counting at zero

                    if (selectedItem == 0){
                          someView = findViewById(R.id.layout);
                          root = someView.getRootView();
                          root.setBackgroundColor((Color.parseColor("#005640")));

                          Color_Position =Color.parseColor("#005640");

                          //prefer_edit.putInt("background_color", ((Color.parseColor("#005640"))));
                          Toast.makeText(getBaseContext(), "Color Changed", Toast.LENGTH_SHORT).show(); 
                          Log.i("At", "Position "+selectedItem);


                }

                    else if (selectedItem == 1){    


                          someView = findViewById(R.id.layout);
                          root = someView.getRootView();
                          root.setBackgroundColor((Color.parseColor("#045345")));

                          Color_Position =Color.parseColor("#045345");

                          //prefer_edit.putInt("background_color", ((Color.parseColor("#045345"))));
                          Toast.makeText(getBaseContext(), "Color Changed", Toast.LENGTH_SHORT).show(); 
                          Log.i("At", "Position "+selectedItem);                     

                }

                    else if (selectedItem == 2){


                          someView = findViewById(R.id.layout);
                          root = someView.getRootView();

                          Color_Position =Color.parseColor("#384355");

                          root.setBackgroundColor((Color.parseColor("#384355")));
                          //prefer_edit.putInt("background_color", ((Color.parseColor("#384355"))));
                          Toast.makeText(getBaseContext(), "Color Changed", Toast.LENGTH_SHORT).show(); 
                          Log.i("At", "Position "+selectedItem);

                }

                    else if (selectedItem == 3){


                          someView = findViewById(R.id.layout);
                          root = someView.getRootView();

                          Color_Position =Color.parseColor("#990088");

                          root.setBackgroundColor((Color.parseColor("#990088")));
                          //prefer_edit.putInt("background_color",((Color.parseColor("#990088")))); 
                          Toast.makeText(getBaseContext(), "Color Changed", Toast.LENGTH_SHORT).show(); 
                          Log.i("At", "Position "+selectedItem);

                }

                    else if (selectedItem == 4){


                          someView = findViewById(R.id.layout);
                          root = someView.getRootView();

                          Color_Position =Color.parseColor("#026211");

                          root.setBackgroundColor((Color.parseColor("#026211")));
                         // prefer_edit.putInt("background_color", (Color.parseColor("#026211")));
                          Toast.makeText(getBaseContext(), "Color Changed", Toast.LENGTH_SHORT).show(); 
                          Log.i("At", "Position "+selectedItem);

                }   


                    return true;

            }       
        });     
    }
  
    

public void onStart(){

         

super.onStart();

    Log.i("Getting Data", "Color is equal to  "+Color_Position);
     SharedPreferences prefer = getSharedPreferences(Color_Value,0);
        prefer.getInt(background_color, 0);
}
    public void onStop(){
super.onStop();

        Log.i("Saving Data", "Color is equal to  "+Color_Position);
        SharedPreferences prefer = getSharedPreferences(Color_Value, 0);
        SharedPreferences.Editor prefer_edit = prefer.edit();
        prefer_edit.putInt(background_color, Color_Position);
        prefer_edit.commit();   

    }
  
     @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.

                getMenuInflater().inflate(R.menu.check_box, menu);
                return true;
            }

            @Override
            public boolean onOptionsItemSelected(MenuItem item) {

                switch (item.getItemId()){

                    case
                    R.id.action_settings:
                    //Pass data between activities here
                break;

                    case 
                    R.id.action_save:

                        SaveData();

                break;


                    case 
                    R.id.action_error:

                        LoadData();
                break;

                    case 
                    R.id.action_edit:

                        ChangeTextColor(change);
                        change++;

                break;

                }


                return true;
            }

            public void SaveData(){

                Message =ed.getText().toString();
                        try {
                            FileOutputStream out = openFileOutput("text.txt", MODE_PRIVATE);
                            OutputStreamWriter output = new OutputStreamWriter(out);
                            try{
                                output.write(Message);
                                output.flush();
                                output.close();
                                Toast.makeText(getBaseContext(), "Save successful", Toast.LENGTH_SHORT).show(); 


                    }
                    catch (FileNotFoundException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                    }

                    catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                    }
                    } 

                        catch (FileNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                    }


                    }

            public void LoadData(){
                try {
                    FileInputStream fis = openFileInput("text.txt");
                    InputStreamReader ins = new InputStreamReader(fis);
                    //Deserialize the file
                    char[] Data = new  char [datablock];
                    String final_data ="";
                    int size;
                    try {
                        while((size = ins.read(Data))>0){
                        String read_data = String.copyValueOf(Data, 0 ,size);
                        final_data+=read_data;
                        Data = new char[datablock];
                        }
                        Toast.makeText(getBaseContext(), "Message : " + final_data, Toast.LENGTH_SHORT).show();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                    }
            //Methods galore!

            public void ChangeTextColor(int change){


                if ((change % 2) == 0) {
            // number is even
                    ed.setHint("enter a message to save");
                    ed.setTextColor(getResources().getColor(android.R.color.holo_red_dark));

                    Toast.makeText(getApplicationContext(), "Text color changed to red", Toast.LENGTH_LONG).show();
                    change++;
                }

                else {
            // number is odd
                    ed.setHint("enter a message to save");
                    ed.setTextColor(getResources().getColor(android.R.color.black));

                    Toast.makeText(getApplicationContext(), "Text color changed to black", Toast.LENGTH_LONG).show();
                    change++;
                }



            }


        }

1 个答案:

答案 0 :(得分:0)

在onCreate之后调用onStart,请删除此

        if (Color_Value!= null){
            onStart();
        }
        else {

        }

您可以直接在onCreate中检索SharedPreferences的颜色。