----------- Shared Preferences Example----------
--------------Appcontroller class------------
package com.example.shirsh.shareprefeferenceexample; import android.app.Activity; import android.content.Context; import android.content.pm.PackageManager; import android.support.multidex.MultiDex; import android.support.multidex.MultiDexApplication; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.text.TextUtils; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import static android.content.pm.PackageManager.*; /** * Created by androidsys1 on 3/31/2017. */ public class AppController extends MultiDexApplication { private static AppController mInstance; public static int invoiceId = 0; public static final String TAG = AppController.class.getSimpleName(); public static synchronized AppController getInstance() { return mInstance; } @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { MultiDex.install(this); super.onCreate(); mInstance = this; } public void handleUncaughtException(Thread thread, Throwable e) { e.printStackTrace(); } }
---------------SavedData class------------
package com.example.meenakshi.shareprefeferenceexample;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public class SavedData {
private static final String SERVICE_ID = "service_id";
private static final String Edittext1 = "edit_1";
static SharedPreferences prefs;
public static SharedPreferences getInstance() {
if (prefs == null) {
prefs = PreferenceManager.getDefaultSharedPreferences(AppController.getInstance());
}
return prefs;
}
public static String getServiceId() {
return getInstance().getString(SERVICE_ID, null);
}
public static void saveServiceId(String service_id) {
SharedPreferences.Editor editor = getInstance().edit();
editor.putString(SERVICE_ID, service_id);
editor.apply();
}
public static String getEdittext1() {
return getInstance().getString(Edittext1, null);
}
public static void saveEdittext1(String edit_1) {
SharedPreferences.Editor editor = getInstance().edit();
editor.putString(Edittext1, edit_1);
editor.apply();
}
}
---------------Splash Screen Activity-----------------
package com.example.meenakshi.shareprefeferenceexample;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Main4Activity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
if(SavedData.getServiceId()==null) {
Intent intent = new Intent(Main4Activity.this, MainActivity.class);
startActivity(intent);
}
else if(SavedData.getServiceId().equals("Activity1")) {
Intent intent = new Intent(Main4Activity.this, MainActivity.class);
startActivity(intent);
}
else if(SavedData.getServiceId().equals("Activity2")) {
Intent intent = new Intent(Main4Activity.this, Main2Activity.class);
startActivity(intent);
}
else if(SavedData.getServiceId().equals("Activity3")) {
Intent intent = new Intent(Main4Activity.this, Main3Activity.class);
startActivity(intent);
}
new Handler().postDelayed(new Runnable() {
@Override public void run() {
}
}, 2000);
}
}
------------------MainActivity----------------
package com.example.meenakshi.shareprefeferenceexample;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import static com.example.meenakshi.shareprefeferenceexample.R.styleable.View;
public class MainActivity extends AppCompatActivity {
EditText firsttext, secondtext, thirdtext, fourthtext;
Button buttonfirstactivity;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firsttext = (EditText) findViewById(R.id.firsttext);
secondtext = (EditText) findViewById(R.id.secondtext);
thirdtext = (EditText) findViewById(R.id.thirdtext);
fourthtext = (EditText) findViewById(R.id.fourthtext);
Log.e("shish", "============");
SavedData.saveServiceId("Activity1");
buttonfirstactivity = (Button) findViewById(R.id.buttonfirstactivity);
buttonfirstactivity.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
SavedData.saveEdittext1(firsttext.getText().toString());
startActivity(intent);
}
});
}
}
----------------------Main2activity--------------------
package com.example.meenakshi.shareprefeferenceexample;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Main2Activity extends AppCompatActivity {
EditText firsttext_secondact, secondtext_secondact, thirdtext_secondact, fourthtext_secondact;
Button buttonfirstactivity_secondact;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
String text = SavedData.getEdittext1();
Log.e("shirsh","++++++++2");
Log.e("", "==============" + text);
SavedData.saveServiceId("Activity2");
firsttext_secondact = (EditText) findViewById(R.id.firsttext_secondact);
secondtext_secondact = (EditText) findViewById(R.id.secondtext_secondact);
thirdtext_secondact = (EditText) findViewById(R.id.thirdtext_secondact);
fourthtext_secondact = (EditText) findViewById(R.id.fourthtext_secondact);
firsttext_secondact.setText(text);
buttonfirstactivity_secondact = (Button) findViewById(R.id.buttonfirstactivity_secondact);
buttonfirstactivity_secondact.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
Intent intent = new Intent(Main2Activity.this, Main3Activity.class);
startActivity(intent);
}
});
}
}