Moving from one activity to another activity in android studio [duplicate] - android

This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
How to use putExtra() and getExtra() for string data
(18 answers)
Closed 5 years ago.
I'm writing an application in android studio IDE which shows the models,brands ,prices and etc of the cars and I want to move from one activity to another one.can I use putExtra?if yes i wonder if anyone would like to tell me how can i use it.

You can pass data between activities in easiest way like this.
In the first activity use out extra to send the data
Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("YOUR_DATA_KEY", data);
startActivity(intent);
Access that intent on next activity like this
String s = getIntent().getStringExtra("YOUR_DATA_KEY");

try this
Intent intent=new Intent(this,YourActivity.class);
intent.putExtra("key",value);
startActivity(intent);

Try the following
Intent intent =new Intent(FirstActivity.this,SecondeActivity.class);
StartActivity(intent);

Related

Android Explicit Intent code [duplicate]

This question already has answers here:
Using Intent in an Android application to show another activity
(11 answers)
Closed 7 years ago.
How can I pass the control from one Activity to another using the explicit Intent?
I had an issue in androidManifest.xmnl as it marked the Mainactivity file As an error.
If you just want to navigate from one activity to another you can use explicit intent as :
Intent next = new Intent(YourCurrentActivity.this , NextActivity.class);
startActivity(next);

how to add a button in the running activity [duplicate]

This question already has an answer here:
Which is the best way to add a button?
(1 answer)
Closed 7 years ago.
I am very new to android development and I am having problem displaying a new button to a activity which has already run. I am using this tutorial http://developer.android.com/training/basics/firstapp/starting-activity.html. Here they display Hello World in the new activity. I would like to add a button called "SKIP"! So that the user can go to a another new activity called Functions. How do I do that with the intent? My intent is already being used to display the string hello world! I am using android studio.
Appreciate any help thanks
To create a button you need to go to your main XML file, add a button. Then in your Activity create a onClick listener for the button.
When it comes to going to another activity you need to create a new Intent with
Intent intent = new Intent(this, Functions.class);
startActivity(intent)
You would call your new activity with the following snippet within the OnClickListener of your button:
Intent intent = new Intent(this, Functions.class);
startActivity(intent);
And define your Functions class in your AndroidManifest.xml file similar to what the originally defined Activity is defined as. But you should only have one launcher Activity.

Retrieving data between more than one Activities [duplicate]

This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 9 years ago.
I am new to android application I have a task is that i should pass data from activity A->Activity B and should pass data between Activity B-----------> Activity C Same way I should get data from Activity C-------> Activity A please tell the scenario or send an example for me
Try two options
1). Intent
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra("KEY", "VALUE");
startActivity(intent);
2). Shared Preference if the data is shared between all activities.
You can pass data from A to B by using intent:
Intent intent = new Intent(this, Activity.class);
intent.putExtra("name", "value");
startActivity(intent);
You can return back value using onActivtyResult() or by storing it in
shared preferences
If you are using onActivtyResult() method you need to start activty for result using : startActivityForResult() method
You can check this for sending data back
send data back to activity
Intent is what you needed:
Intent mIntent = new Intent(ActivityA.this, ActivityB.class);
mIntent.putExtra("key", "value");
startActivity(mIntent);
The above code starts ActivityB and sends the data along with it, to ActivityB (from mIntent.putStringExtra("key", "value");)
You can also see the different prototypes of this from here.
On the next activity, ActivityB, you can get the data sent by code below (write it inside onCreate() in ActivityB):
Intent mIntent = getIntent();
String data_recieved = mIntent.getStringExtra("key"); \\ here key is same as mentioned in previous activity from where you reached here.

Make activities communicate [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I pass data between activities in Android?
I have three activities in my application and each activity is depending on another. I'm currently using static variables to pass objects and values between these activities. Problem with this is that it get's very confusing fast and it's hard to keep track of when I assigned that global variable a value etc. I'm thinking of implementing an interface between these activities to make the code clearer and easier to understand. Thing is, i'm not entirely sure this is the best way to go so any help or advice would be great.
use putExtra to send info to another activity
send:
Bundle bundle = new Bundle();
bundle.putString(“name″, “username”);
Intent newIntent = new Intent(this.getApplicationContext(), ActivityClass2.class);
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);
Receive:
Bundle bundle = this.getIntent().getExtras();
String data = bundle.getString(“name″);
data = username
I believe what you want is the Intent.putExtra() method. There are several methods depending on what kind of data you want to pass. See the documenation here.
to pass data:-
Intent i = new Intent();
i.putExtra("key", "data");
startActivity(i);

how do I restart an activity in android? [duplicate]

This question already has answers here:
How to restart Activity in Android
(22 answers)
Closed 8 years ago.
in an app that I am writing, there is a part of it that allows you to change a curtain setting. the problem is, that this setting won't take effect until the activity is recreated. is there a way to tell the app to restart using the onResume() method (hopefully allowing it to save everything in the onSaveInstanceState())?
This has been posted before:
Intent intent = getIntent();
finish();
startActivity(intent);
As of API level 11, you can also just call an activity's recreate() method. Not only is this cleaner because it is less code, it avoids issues that may arise if your activity was launched by an implicit intent.
Perhaps you could restart the activity as has been demonstrated, but pass in some intent extras to send your string back when it re-starts.
Intent intent = getIntent();
intent.putExtra(STRINGTOSAVE, "Save this string");
finish();
startActivity(intent);
and in your onCreate you would of course want to retrieve the string
Intent intent = getIntent();
String STRINGTOSAVE = intent.getStringExtra(ActivityName.STRINGTOSAVE);
and then use the retrieved string to reapply the textfield and any other actions you need.

Categories

Resources