Saving a string from first run to later start an activity

199 views Asked by At

I have 4 activities in my android app.

1) Splash Activity - To decide if the app is being launched for the first time and to decide which activity to open

2)Main Activity - Button to open camera and start scanning i.e go to QR activity

2) QR Activity - Scan a QR code

3) Web Activity - On successful scanning, open a web page in the app. Use the data from the QR code to make a URL for the web page

In my splash activity, I check if it is the first run. If it is, I got to the main activity and if not, I want to go to the web activity. In my QR activity, I scan QR code and get a number from it. I use this number in the next activity, i.e, web activity to make a url using the scanned number and open the web page, But now, since I want to start different activity depending on the app run number, I want to save the scanned number from the first activity for all future runs of the app. Much like Facebook, which stores our login credentials for all future runs.

I am trying to do something like this, but the scanned value is not passed to my web activity

ScannerActivity.java

public static final String PREFS_NAME = "myPrefs";
if (barcodes.size() != 0) {

                Intent intent = new Intent(getApplication(), WebActivity.class);
                //intent.putExtra("result",barcodes.valueAt(0));

                SharedPreferences.Editor editor=settings.edit();
                editor.putString("result", barcodes.valueAt(0).displayValue);
                editor.commit();

                startActivity(intent);
                finish();
            }

WebActivity.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web);

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    String result = settings.getString("result", "");

    /*Barcode barcode = (Barcode) getIntent().getParcelableExtra("result");
    Toast.makeText(WebActivity.this, barcode.displayValue, Toast.LENGTH_LONG).show();*/

    Toast.makeText(WebActivity.this, result, Toast.LENGTH_SHORT).show();

    webView = (WebView) findViewById(R.id.webview);
    webView.setWebViewClient(new myWebClient());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl(" http://url?u="+result);

}
3

There are 3 answers

1
Khaledonia On

You're recommended to visit Android Studio Storage Option to have an enlarged perception of the mechanism and functionality

that being said, allow me to provide you with a snippet of how I store values in my application

Note you need to make minor adjustments, since I am applying Sharedpreferences inside a fragment

first

   addusername = (EditText) oView.findViewById(R.id.addnewUsername);

second

  //adjustment and reattachment
  String bonsiour = addusername.getText().toString().trim();

            SharedPreferences sPref = getActivity().getPreferences(0);
            SharedPreferences.Editor edt = sPref.edit();
            edt.putString("key1", bonsiour);
            edt.commit();


            //toast to confirm value has been saved
            String buzo = sPref.getString("key1", "empty");
            Toast.makeText(getActivity(), "You're  " + buzo + "!!", Toast.LENGTH_LONG).show();

This is how to extract/read from it

  SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
    String name = prefs.getString("key1", "No name defined");

    if(name.equals("PDF_FOUND")){
        Toast.makeText(Controller.this,"IT WORKED !", Toast.LENGTH_SHORT).show();

   //skip the splash screen and move to another activity asap



    } else{
       Toast.makeText(Controller.this,"BAD NEWS !", Toast.LENGTH_SHORT).show();


  //display Splash screen

 }    
}
1
yogur On

To store your String in shared preferences, you can do the following in a one liner:

String QRCode = "code";
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("QRCode", QRCode).commit();

To retrieve the value of your stored String, use this:

//The second parameter for getString() is the default value to return if QRCode is not set 
String QRCode = PreferenceManager.getDefaultSharedPreferences(this).getString("QRCode", "NOT_FOUND");

if(QRCode.equals("NOT_FOUND")){
    //open MainActivity
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
} else {
    //open WebActivity
    Intent intent = new Intent(this, WebActivity.class);
    //you can pass code to webactivity or retrieve it from shared prefs
    intent.putExtra("QRCode", QRCode);
    startActivity(intent);
}
1
Jackyto On

You can use SharedPreferences to store your String and re-use it later

If you want to store a String for example, first create keys to get your values

public static final String         sharedPreferencesKey = "shareKey";
public static final String         stringKey = "stringKey"; 

To store your value

SharedPreferences sharedpreferences =
       context.getSharedPreferences(sharedPreferencesKey, Context.MODE_PRIVATE);
SharedPreferences.Editor ed = sharedpreferences.edit();
ed.putString(stringKey, "");
ed.apply();

To get your value

if (sharedpreferences.contains(stringKey))
    sharedPreferences.getString(stringKey, "")