Multiple Intents in a Single Activity - java

Having issues with sending multiple pieces of data (in this case, three arrays(two int, one string)) over to a second activity page.
I am unsure of how this is done. What I would like to know is how to send these arrays in one Start Activity method, if that is possible. My current code is:
public void onClickGoToTeamSummary(View view)
{
Intent intentTeamNames = new Intent(MainActivity.this, ResultsActivity.class);
Intent intentTeamPoints = new Intent(MainActivity.this, ResultsActivity.class);
Intent intentTeamGoals = new Intent(MainActivity.this, ResultsActivity.class);
intentTeamNames.putExtra("footballClubs", myTeams);
intentTeamPoints.putExtra("clubPoints", pointsAttained);
intentTeamGoals.putExtra("clubGoals", goalsScored);
startActivity(intentTeamPoints);
startActivity(intentTeamNames);
startActivity(intentTeamGoals);
}
I had tried:
startActivity(intentTeamPoints, intentTeamNames, intentTeamGoals);
to no avail. To help, my getIntent in the next activity looks like this:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results);
Intent intentClubNames = getIntent();
String[] club_names = intentClubNames.getStringArrayExtra("footballClubs");
Intent intentClubPoints = getIntent();
int[] team_points = intentClubPoints.getIntArrayExtra("clubPoints");
Intent intentTeamGoals = getIntent();
int[] club_goals = intentTeamGoals.getIntArrayExtra("clubGoals");
}
The code itself works provided only one startActivity is used. I would like to know how to pass all my arrays into the second activity page through one activity if anyone can help me.

Try this..
You can send all values in single Intent
public void onClickGoToTeamSummary(View view)
{
Intent intentTeamNames = new Intent(MainActivity.this, ResultsActivity.class);
intentTeamNames.putExtra("footballClubs", myTeams);
intentTeamNames.putExtra("clubPoints", pointsAttained);
intentTeamNames.putExtra("clubGoals", goalsScored);
startActivity(intentTeamNames);
}
and
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results);
Intent intentClubNames = getIntent();
String[] club_names = intentClubNames.getStringArrayExtra("footballClubs");
int[] team_points = intentClubNames.getIntArrayExtra("clubPoints");
int[] club_goals = intentClubNames.getIntArrayExtra("clubGoals");
}

You don't use multiple Intents but multiple extras.
// create your Intent as normal
Intent myIntent = new Intent(MainActivity.this, ResultsActivity.class);
// then you can add multiple extras
myIntent.putExtra("footballClubs", myTeams);
myIntent.putExtra("clubPoints", pointsAttained);
myIntent.putExtra("clubGoals", goalsScored);
startActivity(myIntent);
Then receiving them would be the same. You would just receive the one Intent and use the key as normal for each extra Array.

Related

How to transfer url from one class to another activity

In my app urls are getting from the firebase database . I want to send these url to the another activity. how can i pass the url (Uri webpage) to the
viewer class.Please tell me how to do it.
Use intent in first activity like this:
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("key", value);
CurrentActivity.this.startActivity(intent);
And retrieve it on the second activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
String value = intent.getStringExtra("key");
}
you can use this SharedPreferences
SharedPreferences sharedPref = getSharedPreferences("key",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("key1","your uri");
editor.apply();
if you want get value in new ativity
SharedPreferences sharedPref = getSharedPreferences("key",Context.MODE_PRIVATE);
String uri=sharedPref.getString("key1","default value if value is null");
keys values must be same
You can also send String Array via intent from one activity to another.
In sender's activity, you can add URL string array into intent.
Intent intent = new Intent(this, MainActivity.class);
String[] urls = new String[] {
"https://google.com",
"https://stackoverflow.com"
};
intent.putExtra("urls", urls);
startActivity(intent);
In MainActivity (Receiver), you can get String array from intent.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] urls = getIntent().getStringArrayExtra("urls");
}
}
Hope it will be helpful.

Android Studio: How to send, and recieve a Class in diferent Activities

Im making a quiz game with questions on diferent topics
For Example i have activities for these topics: Flags, Capitals, Population, Economy, Continent, etc.
And i have one single ResultActivity to obtain the Score of the quiz.
The ResultActivity has a PLAY AGAIN button.
On the FlagsActivity i have this code:
Intent intent = new Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra("RIGHT_ANSWER_COUNT", score);
intent.putExtra("NAME_ACTIVITY", "FlagsActivity");
startActivity(intent);
On the CapitalActivity i have this code:
Intent intent = new Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra("RIGHT_ANSWER_COUNT", score);
intent.putExtra("NAME_ACTIVITY", "CapitalActivity");
startActivity(intent);
etc.....
On the ResultActivity i have this code:
activity = getIntent().getStringExtra("NAME_ACTIVITY");
public void playAgain(View view){
if(activity.equals("FlagsActivity")){
Intent intent = new Intent(getApplicationContext(), FlagsActivity.class);
startActivity(intent);
}
if(activity.equals("CapitalActivity")){
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
if(activity.equals("PopulationActivity")){
Intent intent = new Intent(getApplicationContext(), PopulationActivity.class);
startActivity(intent);
}
if(activity.equals("EconomyActivity")){
Intent intent = new Intent(getApplicationContext(), EconomyActivity.class);
startActivity(intent);
}
if(activity.equals("ContinentActivity")){
Intent intent = new Intent(getApplicationContext(), ContinentActivity.class);
startActivity(intent);
}
}
Basically im sending an Intent with a String containing the name of the activity, then on the Result Activity evaluating with "if" the String = That activity name, start the activity.
What i want to do is someting like this:
On the Flags Activity:
Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra(FlagsActivity.class);
startActivity(intent);
On the CapitalActivity:
Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra(CapitalActivity.class);
startActivity(intent);
On the Result Activity:
activity = getIntent();
public void playAgain(View view){
Intent intent = new Intent(getApplicationContext(), activity);
startActivity(intent);
}
So that i can create as many quiz activities without having to create an "if" statement on the ResultActivity for it to work.
You can pass the class name as a string and use reflection to look up a class object for that type. Something like this:
Class classToLoad = Class.forName(getIntent().getStringExtra("NAME_ACTIVITY"));
Intent intent = new Intent(getApplicationContext(), classToLoad);
startActivity(intent);
I would pass the fully qualified class name to avoid errors.
You could pass the String of activity name to the Result activity, and get its corresponding class name using reflection:
String strActivity = "com.package.FlagsActivity";
Intent intent = new Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra("activity_name", strActivity );
startActivity(intent);
String activityName = getIntent().getStringExtra("activity_name");
Class<?> myClass = Class.forName(activityName );
Intent myIntent = new Intent(getApplicationContext(), myClass);
Something like this.
In the Results Activity
public static void toActivity(Context context, final Class ActivityToOpen){
//whatever awesome code you would like to perform
Intent intent = new Intent(context, ActivityToOpen);
startActivity(intent);
}
In the originating Activity (Flags Activity for example)
ResultsActivity.toActivity(FlagsActivity.this, FlagActivity.class);
Check for typos... I kinda winged this :)
You should be able to call from any originating Activity without if statement.
When you receive the data
String activity;
Bundle bundle = getIntent().getExtras();
if (bundle != null){
activity = bundle.getString("NAME_ACTIVITY");
}

How to get value of a TextView which is in another activity?

I want to get the value of a TextView which is defined in another activity? I am using android studio.
You should use intents for passing values to another activities.
in first activity:
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("YOURKEY", yourTextViewValue);
startActivity(intent);
Access that intent on next activity
String textViewValue= getIntent().getStringExtra("YOURKEY");
First activity (which has TextView)
SecondActivity.launchActivity(this, textView.getText().toString())
Second Activity (which need the text value)
public static void launchActivity(Context context, String textViewValue) {
Intent intent = new Intent(context, SecondActivity.class);
intent.putExtra("textValueKey", textViewValue)
context.startActivity(intent);
}
#Override
public void onCreate(Bundle savedInstanceState) {
if (getIntent() != null) {
String textViewValue = getIntent().getStringExtra("textValueKey");
}
...
}

Passing data from one activity to another and then printing

The problem I am having is that it prints out Null on the second activity and not the actual username that is entered. Is the data being passed to the second activity correctly? Does the second activity need more code? Sorry but not the best at programming.
I have this code in my main class
if (username.getText().toString().equals("batman") &&
password.getText().toString().equals("Joker")) {
Toast.makeText(MainActivity.this, "Username and
password is correct", Toast.LENGTH_SHORT).show();
Intent intent = new Intent("com.example.*******.loginpage.User");
intent.putExtra("username",String.valueOf(username));
startActivity(new Intent(MainActivity.this, User.class));
This is the code inside my second class.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
Intent intent = getIntent();
String username = getIntent().getStringExtra("username");
TextView textView = (TextView) findViewById(R.id.textView4);
textView.setText("Welcome" + " " + username );
The problem is your intent in your first class
Intent intent = new Intent("com.example.*******.loginpage.User"); <-- have created an intent
intent.putExtra("username",String.valueOf(username));
startActivity(new Intent(MainActivity.this, User.class)); <-- but using new Intent
You have created an intent but you passing new intent. Use your created Intent instead of passing new Intent.
Intent intent = new Intent(MainActivity.this, User.class);
intent.putExtra("username",String.valueOf(username));
startActivity(intent);
EDIT
Instead using String.valueOf(username) you must use username.getText(), because String.valueOf(username) is method to translate your object to String.
Intent intent = new Intent(MainActivity.this, User.class);
intent.putExtra("username",username.getText());
startActivity(intent);
Two problems here.
First one is that you have to pass the intent where you put your extra instead of creating new one to startActivity, like
Intent intent = new Intent(MainActivity.this, User.class);
intent.putExtra("username",username.getText().toString());
startActivity(intent);
Second problem is that username looks like editText, String.valueOf won't pass actual value, use username.getText().toString() like i mentioned in code.

Android: Set Text View's text from another activity Not working

I have 2 activities and 2 classes.
In my Main class, when i click submit button it will start another activity. here is the code.
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, NewActivity.class));
newActivity.setViewValues(fNameET.getText().toString(), lNameET.getText().toString(), mInitialET.getText().toString(), "Female", "birthday", addressET.getText().toString(), cNumberET.getText().toString());
}
The newActivity is an object of the other activity and the setViewValues is the method of it.
This doesn't work, this is how i do it in java gui. Maybe something is missing.
Could anyone help me with this?
You should pass the data like this.
MainActivity.java
Intent intent = new Intent(MainActivity.this, NewActivity.class));
intent.putExtra("firstName",fvalue);
intent.putExtra("lastname",lname);
......
startActivity(intent);
NewActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutname);
Intent intent = getIntent();
String firstName = intent.getStringExtra("firstName");
String lastName = intent.getStringExtra("lastname");
}

Categories

Resources