How can I send information from one activity to two activities? I would like to add another intent but I get an error about a null object... My guess is that I also would need to use another spinner for this class but just wondering if there is a short cut?
add_review.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Review.this, Add_Review.class);
String chosenOption = spinner.getSelectedItem().toString();
intent.putExtra("Add a Review for", chosenOption);
startActivity(intent);
finish();
Intent send_information = new Intent(Review.this, PopularMedicalClinic.class);
String chosenOption2 = spinner.getSelectedItem().toString();
intent.putExtra("Name", chosenOption2);
startActivity(send_information);
finish();
}
This is a wrong approach in your code. You are trying to open two activity at a time. After starting first intent you call finish() method. finish() method kill current activity.
Can you explain why you want to open two activity at a time. Then I can suggest you.
Just use another extra putExtra method.
and remove finish() method. it will destroy your activity.
String chosenOption = spinner.getSelectedItem().toString();
String chosenOption2 = spinner.getSelectedItem().toString();
Intent intent = new Intent(Review.this, Add_Review.class);
intent.putExtra("Add_a_Review_for", chosenOption);
intent.putExtra("Name", chosenOption2);
startActivity(intent);
Related
Main Activity 2
MAIN ACTIVITY
Android Studio My name is not displaying in second activity after passing through first Activity
In your Main Activity:
Please call intent.putExtra(), before startActivity(). Example:
val intent = Intent(this, NewActivity::class.java)
intent.putExtra("username","Name")
startActivity(intent)
And fetch like that (in Main Activity 2)
val bundle :Bundle?=intent.extras
if (bundle!=null){
val user_name = bundle.getString("username")
}
I noticed that in the 'MainActivity.java' you are starting new activity before passing the string into the intent,
just switch the both lines 30 and 31.
In your MainActivity, you should putExtra before startActivity. Like this:
Intent intent = new Intent(...);
intent.putExtra...;
startActivity(intent);
In your MainActivity, create a new Intent:
String name="abcdef;
Intent i = new Intent(MainActivity.this, MainActivity2.class);
i.putExtra("key",name);
startActivity(i);
Then in the MainActivity2, retrieve those values:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String name = extras.getString("key");
//"key" must match that used in the other activity
}
I have A SharedPreference That I want to save an array list for the next time I open the app. But The next time I start the app it comes up as empty. I execute the save onDestroy because I dont want to push a button. I dont know whats going on. Any help would be appreciated.
I run LoadData() Method inside onCreate() Method.
public void LoadData(){
SharedPreferences sp = getSharedPreferences("SavedArrayList",MODE_PRIVATE);
Gson gson = new Gson();
String json = sp.getString("Arraylist", null);
Type type = new TypeToken<ArrayList<ListItems>>() {}.getType();
if (json != null)items = gson.fromJson(json, type);
}
#Override
protected void onDestroy() {
SharedPreferences sp = getSharedPreferences("SavedArrayList",MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
Gson gson = new Gson();
String json = gson.toJson(items);
editor.putString("Arraylist", json);
editor.apply();
super.onDestroy();
}
All I did was moved it to onPause and onResume now it works fine.
I have two Activity. I want pass LatLng pass to another Activity.
This code in first Activity:
public void testclick (View v){
Intent intent = new Intent(TestActivity.this, MainMenu.class);
intent.putExtra("lat", "58.37");
intent.putExtra("lan", "37.95");
startActivity(intent);
}
What I must write in another Activity to get LaTLng?
Intent intent = new Intent(getApplicationContext(), MainMenu.class);
intent.putExtra("lat", "`58.37`");
intent.putExtra("lan", "37.95")
startActivity(intent)
And MainMenu Activity onCreate() method receive
if(getIntent().getExtras() != null) {
String lat = getIntent().getExtras().getString("lat");
String lan = getIntent().getExtras().getString("lan");
}
I want to open a new activity when search filter is clicked in recycler view of android studio.
An open source code may be found here that is given below. It has json file from where we can fetch the search filter item.
https://www.androidhive.info/2017/11/android-recyclerview-with-search-filter-functionality/
Please tell me the code that how we can get to new activity such as hello.xml when hello is being clicked in search filter item.
In my opinion the code should be here in mainactivity.java..
#Override
public void onContactSelected(Contact contact) {
String type = contact.getClass().getName();
if (type.startsWith("Tom")) {
Intent i = new Intent(MainActivity.this, MainActivity.class);
startActivity(i);
} else if (type.startsWith("")) {
Intent intent = new Intent(MainActivity.this, Hello.class);
startActivity(intent);
}
Please help to put my code instead of toast.
public void onContactSelected(Contact contact) {
// TextView name = (TextView) Contact;
if ( contact.getName().toString().equalsIgnoreCase("Tom Hardy"))
{
startActivity(new Intent(MainActivity.this, Hello.class));
finish();
}
else{
Toast.makeText(MainActivity.this, " no activity "+contact.getName(), Toast.LENGTH_SHORT).show();
}
}
I have a main Activity (OceanintelligenceActivity). In this activity I register the device for push notifications and also I registered a receiver that shows a Dialog and starts the proper Activity depending on the info sent from my server. This is the code I'm using to register the device and the receiver :
protected void gcmRegistration(){
PMApplication thisApp = PMApplication.getInstance();
AppDelegate delegate = thisApp.getAppDelegate();
final Context context = this;
// Make sure the device has the proper dependencies.
GCMRegistrar.checkDevice(this);
// Make sure the manifest was properly set - comment out this line
// while developing the app, then uncomment it when it's ready.
GCMRegistrar.checkManifest(this);
// Let's declare our receiver
registerReceiver(mHandleMessageReceiver,new IntentFilter(DISPLAY_MESSAGE_ACTION));
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
Log.d("", "Lets register for Push");
GCMRegistrar.register(this, SENDER_ID);
}else {
if(GCMRegistrar.isRegisteredOnServer(this)) {
// Skips registration.
String apnsToken = delegate.sso.getAPNSToken();
if(!apnsToken.equals(regId)){
Log.d("", "The Device RegId has changed on GCM Servers");
// We should let our servers know about this
ServerUtilities.update(regId, context);
}
} else {
Log.d("","Is not register on PM Server");
// Try to register again, but not in the UI thread.
// It's also necessary to cancel the thread onDestroy(),
// hence the use of AsyncTask instead of a raw thread.
mRegisterTask = new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
boolean registered = ServerUtilities.register(context, regId);
// At this point all attempts to register with the app
// server failed, so we need to unregister the device
// from GCM - the app will try to register again when
// it is restarted. Note that GCM will send an
// unregistered callback upon completion, but
// GCMIntentService.onUnregistered() will ignore it.
if (!registered) {
GCMRegistrar.unregister(context);
}
return null;
}
#Override
protected void onPostExecute(Void result) {
mRegisterTask = null;
}
};
mRegisterTask.execute(null, null, null);
}
}
}
This is how I set the receiver:
private final BroadcastReceiver mHandleMessageReceiver =
new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
Log.d("","BroadcastReceiver onReceive");
notificationIntent = GCMIntentService.getNotificationIntent(context);
new AlertDialog.Builder(context)
.setMessage(newMessage+". Would you like to see it right now?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Show update
startActivity(notificationIntent);
}
})
.setNegativeButton("No", null).show();
}
};
GCMIntentService.getNotificationIntent(context). This line returns the the Intent with the Activity I want to start.
Whenever there is a notification onReceive gets called but the Dialog only shows if I am on the main activity. So if the app is on a different activity, onReceive still gets called but the dialog doesn't show and therefore I can't start the proper activity.
How can I display a dialog on Currently visible activity on BroadcastReceiver?
Playing around with this one and searching on google I came across a solution. It is not the best one but it works. I still can't believe there is not an easy way to get the current context in Android. So this is what I did to manage to show the Dialog regardless of what the current activity is : I have a public static property of type Context on my singleton class(AppDelegate) and on each activity I override the onResume method and set the Context to the current activity like this AppDelegate.CURRENT_CONTEXT = this. Then on my dialog : AlertDialog.Builder(AppDelegate.CURRENT_CONTEXT).....