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);
}
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
second
This is how to extract/read from it