If I have two activities Activity1 and Activity2 and I want to send data from Activity1 to Activity2 without Start Activity2
I know if I want to start Activity2 I use this code in Activity1.java
Intent intent ;
Bundle bundle = new Bundle();
bundle.putString(BG_SELECT, hexColor);
intent = new Intent(getApplicationContext(), Activity2.class);
intent.putExtras(bundle);
// this is to start but I want just refresh Activity2 not start it
startActivityForResult(intent, uniqueNo);
and in Activity2.java
bundle = getIntent().getExtras();
if (bundle != null) {
bgColor = bundle.getString(SebhaActivity.BG_SELECT);
System.out.println("in Activity2, selected BG: "+bgColor);
}
How to refresh Activity2 to find data in it without Start it? Thanks in Advance.
To run the app from android studio, open one of your project's activity files and click Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen – This example demonstrate about How to send data from one activity to another in Android using sharedPreferences.
In this Example, one EditText is used to input the text. This text is sent to the second activity when the “Send” button is clicked. For this, Intent will start and the following methods will run: putExtra () method is used for send the data, data in key value pair key is variable name and value can be Int, String, Float etc.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.java Step 4 − Add the following code to res/layout/activity_second.xml.
Choose the empty activity, then click Next. Put the activity name and layout name. Android Studio basically takes the Java class name that you provide in the activity name and click Finish. Go to activity_main.xml and click the text button. This XML file contains the designing code for the Android app.
if the next activity (where you need data, Activity2 i.e) wont start from here you can save the data in SharedPreferences in Activity 1 and access it in activity2 when you get there
You can use the LocalBroadcastManager
. For your scenario, i.e., Activity1 sends something to Activity2, you would do something like the following.
# Activity1
Intent intent = new Intent("INTENT_NAME").putExtra(BG_SELECT, hexColor);
LocalBroadcastManager.getInstance(Activity1.this).sendBroadcast(intent);
On Activity2, you would need first to register the receiver, for instance, on its onCreate method, then get the intent using a BroadcastReceiver as following
# Activity2
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, new IntentFilter("INTENT_NAME"));
}
Then, register the mReceiver
to retrieve the BG_SELECT
field
# Activity2
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String receivedHexColor = intent.getStringExtra(BG_SELECT);
}
};
I think here SharedPreferences is become unbeatable.
SharedPreferences is application specific, i.e. the data is lost on performing one of the following options: PREFS_NAME is the name of the file. mode is the operating mode. editor.commit() is used in order to save changes to shared preferences
Your Code goes like this
In First Activity From where you share data
SharedPreferences preferences=getSharedPreferences("PhoneBook",MODE_PRIVATE);
SharedPreferences.Editor editor=preferences.edit();
editor.putInt("registration_id",c.getInt(c.getColumnIndex("db_regiser_id")));
editor.putBoolean("IsLogin",true);
editor.commit();
In Second Activity get data like this
SharedPreferences preferences=getSharedPreferences("PhoneBook",MODE_PRIVATE);
int registration_id = preferences.getInt("registration_id", 0);
Use of SharedPreferences is quite safe.
Also visit
https://developer.android.com/training/data-storage/shared-preferences.html
Hope this one will be helpful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With