0

i want privacy policy , disclaimer statement and a color picker as 1 time run activity when the app is installed . So how can i use persistent storage so that when we install and run the app for the first time privacy policy runs then clicking on button(next) goes to disclaimer activity then to color picker and finally to main activity. And when second time app is run it directly goes to main activity.

 MAIN ACTIVITY:

public static final String PREFS_NAME = "MyPrefsFile"; // Name of prefs file; don't   change this after it's saved something

//public static final String PREFS_NAME = "MyPrefsFile"; 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     

      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); // Get preferences file (0 = no option flags set)
      boolean firstRun = settings.getBoolean("firstRun", false); // Is it first run? If not specified, use "true"

        if (firstRun == false) {
            Log.w("activity", "first time");
            setContentView(R.layout.activity_gasfurnancediagnostics);

            SharedPreferences.Editor editor = settings.edit(); // Open the editor for our settings
            editor.putBoolean("firstRun", false); // It is no longer the first run
            editor.apply(); // Save all changed settings
            return;
        }  else {
            Log.w("activity", "second time");
            checkColor();
            setContentView(R.layout.activity_theme);
            return;
        } 
       // setContentView(R.layout.activity_main);


        //*** CHECKING COLOR IF IT WAS SET PREVIOUSLY ***/

}





    public void checkColor()
    {
        // INITIALIZING THE SHARED PREFRENECES
        SharedPreferences pref = getApplicationContext().getSharedPreferences("Colors", 0);

        // CHECK IF WE HAVE COLOR_SET VALIRABLE IF NOT DEFAULT WILL BE 0
        int color_set = pref.getInt("color_set", 0);

        if(color_set==0){
              Intent e = new Intent(getApplicationContext(), ThemeActivity.class);
                startActivity(e);
                return;


        }
        if(color_set==1)
        { // IF IT IS ALREADY SET IN THE PREVIOUS THAN THIS CAN BE USED FOR REDIRECTING TO OTHER CONTROLLER AND THE BELOW FUNCTIONS CAN BE USED IN THAT CONTROLLER FOR COLOR MODIFICATIUON

            String color = pref.getString("color", null);  // COLOR CODE

            ActionBar mActionBar = getActionBar(); // GETTING ACTIONBAR
            //final Button testbutn = (Button) findViewById(R.id.testbtn);  // GETTING BUTTON

            if (color.equals("red")) {
                mActionBar.setBackgroundDrawable(new ColorDrawable(0xFFBA1E1E));  // CHANGES ACTION BAR COLOR
               // testbutn.setBackgroundColor(0xFFFF6666); 
                // CHANGES BUTTON COLOR

            } else if (color.equals("blue")) {
                mActionBar.setBackgroundDrawable(new ColorDrawable(0xFF21a4dd));  // CHANGES ACTION BAR COLOR
              //  testbutn.setBackgroundColor(0xFFFF4B7E); // CHANGES BUTTON COLOR

            }

            else if (color.equals("yellow")) {
                mActionBar.setBackgroundDrawable(new ColorDrawable(0xFFF6D72B));  // CHANGES ACTION BAR COLOR
              //  testbutn.setBackgroundColor(0xFFFF4B7E); // CHANGES BUTTON COLOR

            }






                mActionBar.setDisplayShowTitleEnabled(false); // THESE TWO STEPS ARE REQUIRED AFTER CHANGING THE ACTION BAR COLOR
                mActionBar.setDisplayShowTitleEnabled(true); // THESE TWO STEPS ARE REQUIRED AFTER CHANGING THE ACTION BAR COLOR

     }









    GAS FURNANCE ACTIVITY

       Button btnnext = (Button)findViewById(R.id.nextbtn);      
       btnnext.setOnClickListener((OnClickListener) this); 
       }

        public void onClick(View v) {
            // TODO Auto-generated method stub
            //Toast.makeText(parent.getContext(), "Button Clicked"+ dataModel.getName(),Toast.LENGTH_LONG).show();
            //Intent yes= new Intent(parent.getContext(), yes(dataModel.getName().class));
            switch(v.getId())
            {
            case R.id.nextbtn:

                 Intent a = new Intent(getApplicationContext(), MainActivity.class);
                 startActivity(a);
                 break;            


                default:


            }

        }











 package com.example.gasfurnancediagnostics;  


import android.app.Activity;  
import android.content.Intent;
import android.content.SharedPreferences; 
import android.graphics.Color;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.Button;

public class ThemeActivity extends Activity implements  ColorPicker.OnColorChangedListener {  
 /** Called when the activity is first created. */  
 @Override  
 public void onCreate(Bundle savedInstanceState) {  
 super.onCreate(savedInstanceState);  
 setContentView(R.layout.activity_theme);  
 Button blueBtn = (Button) findViewById(R.id.bluebtn);

blueBtn.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
    //***** PERSISTANCE STORAGE INITIALIZATION****/
      SharedPreferences pref = getApplicationContext().getSharedPreferences("Colors",  0);

      // **** PERSISTANCE STORAGE EDITOR *** /
      SharedPreferences.Editor editor = pref.edit();

      editor.putString("color", "blue");

      //**** DEFINING THE VALUE THAT THE COLOR IS ALREADY SET SO THAT WE CAN OMIT THIS ACTIVITY ***/
      editor.putInt("color_set", 1);

      //*** COMMITING ALL THE DETAILS TO THE STORAGE...
      //** NOTE WITHOUT THIS THE DATA WONT BE SAVED
      editor.commit();
      Intent e = new Intent(getApplicationContext(), MainActivity.class);
      startActivity(e);


  }
 });

 Button redBtn = (Button) findViewById(R.id.redbtn);

 redBtn.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {

    //***** PERSISTANCE STORAGE INITIALIZATION****/
      SharedPreferences pref = getApplicationContext().getSharedPreferences("Colors", 0);

      // **** PERSISTANCE STORAGE EDITOR *** /
      SharedPreferences.Editor editor = pref.edit();

      editor.putString("color", "red");

      //**** DEFINING THE VALUE THAT THE COLOR IS ALREADY SET SO THAT WE CAN OMIT THIS ACTIVITY ***/
      editor.putInt("color_set", 1);

      //*** COMMITING ALL THE DETAILS TO THE STORAGE...
      //** NOTE WITHOUT THIS THE DATA WONT BE SAVED
      editor.commit();
      Intent e = new Intent(getApplicationContext(), MainActivity.class);
      startActivity(e);


  }
 });

 Button yellowBtn = (Button) findViewById(R.id.yellowbtn);

yellowBtn.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
    //***** PERSISTANCE STORAGE INITIALIZATION****/
      SharedPreferences pref = getApplicationContext().getSharedPreferences("Colors", 0);

      // **** PERSISTANCE STORAGE EDITOR *** /
      SharedPreferences.Editor editor = pref.edit();

      editor.putString("color", "yellow");

      //**** DEFINING THE VALUE THAT THE COLOR IS ALREADY SET SO THAT WE CAN OMIT THIS ACTIVITY ***/
      editor.putInt("color_set", 1);


      //*** COMMITING ALL THE DETAILS TO THE STORAGE...
      //** NOTE WITHOUT THIS THE DATA WONT BE SAVED
      editor.commit();
      Intent e = new Intent(getApplicationContext(), MainActivity.class);
      startActivity(e);



  }
 });

}







@Override
public void colorChanged(String key, int color) {
// TODO Auto-generated method stub

}   

}

1 Answers1

0

SharedPreferences is what you are looking for,add this piece of code in your very first activity

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
  Boolean firstRun = preferences.getString("FirstRun",true);

  if(true)
  {
    /*Open your activity here which you want to open for only once*/
     SharedPreferences.Editor editor = preferences.edit();
     editor.putBoolean("FirstRun",false);
     editor.apply();
  }
  else
  {
   /*Open your activity which you want to open later,generally*/
   }

See this for detailed explaination and this

Community
  • 1
  • 1
nobalG
  • 4,544
  • 3
  • 34
  • 72