Passing strings between activities in android
In your LocationActivity class:
Intent i = new Intent(this, FindAndroidActivity.class);
i.putExtra("KEY",YourData);
In FindAndroidActivity class
Bundle extras = getIntent().getExtras();
if(extras !=null) {
String value = extras.getString("KEY");
}
Passing strings between activities in Android studio
Method invocation 'activityButton.setOnClickListener(new View.OnClickListener() { public void onClick(View... may produce java.lang.NullPointerException
This means that if in this line:
Button activityButton = (Button) findViewById(R.id.Button);
the operation findViewById(R.id.Button)
returns null
, the next line will throw a NullPointerException
because activityButton
value will be null
. This could happen if the id
passed to findViewById
is not found in the layout
.
You can check if activityButton
is not null
and the message will not show up:
Button activityButton = (Button) findViewById(R.id.Button);
if (activityButton != null){
activityButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent startIntent = new Intent(MainActivity.this, SecondaryActivity.class);
startIntent.putExtra("com.example.owner.Message","Hello SecondaryActivity");
startActivity(startIntent);
}
});
}
The second problem: you didn't define a variable name in this line:
Intent = getIntent();
You need to name a variable and then use it in the next line:
Intent intent= getIntent();
String message = intent.getStringExtra("com.example.owner.MESSAGE");
Passing strings between activities, intents not working
You are calling launchHomeActivity()
after getting Response on OnResponse()
and accessToken
. But you stored the token
on createIntent()
which is never called. So call createIntent()
on OnResponse()
method after getting token
.
And this is not best practice to pass token from Activity
to Activity
. Better would be store the token
on SharedPrefrence
and access it from anywherer . Make a singleton SharedPreference
class and you can use it on whole application.
How do I pass data between Activities in Android application?
The easiest way to do this would be to pass the session id to the signout activity in the Intent
you're using to start the activity:
Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent);
Access that intent on the next activity:
String sessionId = getIntent().getStringExtra("EXTRA_SESSION_ID");
The docs for Intents has more information (look at the section titled "Extras").
how to pass String Value of One Activity and Can Use in Any activity in android
I suggest to use SharedPreferences
It is really easy to use and you can fetch data in any activities
here is a good explanation of it
Hope it helps you
Pass Strings through several activities
The app crashes when it should open the second activity. It's the following error that makes me believe this is related to the path: "E/AndroidRuntime(7115): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fm/com.fm.mondev.MeanSelection}: java.lang.NullPointerException: println needs a message"
println needs a message has NOTHING to do with activity anything. Read your logcat!
I get this error when I try to Log
a null value, i.e.
String foo = emptyBundle.getString("barValue");
Log.d(TAG, foo) // error, Log output functions cannot output null values.
Log.d(TAG, "barValue: " + barValue); // outputs "barValue: null"
Make sure whatever you're Logging or System.out.printlning has a value.
How to pass strings in multiple activities
Forward all Extras
You can "forward" all extras using putExtras(intent):
Intent third = new Intent(this, ThirdActivity.class);
third.putExtras(getIntent());
third.putExtra("THIRD", myString);
This will maintain the same key at every step, so if you backtrack (Activity 5 => Activity 3) you can still extract the relevant values for that activity.
Bonus: Use constants to avoid typos
Since your extras might be read in multiple Activities, I would define constants in the final Activity in your flow (I assume a summary or confirmation activity):
public class LastActivity extends AppCompatActivity {
public static final String EXTRA_NAME = "EXTRA_NAME"
public static final String EXTRA_EMAIL = "EXTRA_EMAIL"
public static final String EXTRA_BOOKING_ID = "EXTRA_BOOKING_ID"
// rest of class
}
Then you don't risk typos when you putExtra
or getXxxExtra
:
String name = intent.getStringExtra(LastActivity.EXTRA_NAME);
Related Topics
Integrate Zxing in Android Studio
Android Viewmodel Additional Arguments
Disable Orange Outline Highlight on Focus
How to Get a Dialog Style Activity Window to Fill the Screen
How to Measure Height, Width and Distance of Object Using Camera
How to Filter Android Logcat by Application
Set Span for Items in Gridlayoutmanager Using Spansizelookup
How to Add APKs in an Aosp Build
Error: Resource Android:Attr/Fontvariationsettings Not Found
Causing Outofmemoryerror in Frame by Frame Animation in Android
Android: Remove All the Previous Activities from the Back Stack
Keep a Service Running Even When Phone Is Asleep
How to Get Hosting Activity from a View
Disable Edittext Blinking Cursor
How to Represent a "Many to Many" Relation with Android Room When Column Names Are Same
Expandablelistview - Hide Indicator for Groups with No Children
Material Design Not Styling Alert Dialogs
Android: When Should I Use a Handler() and When Should I Use a Thread