Code to Switch to another java class on android java - java

I have a java class that loads the first xml layout and the second java class that loads another xml. layout
If I am about to switch from my first java class to the second one using code. How would I do it?

You have to use intents to switch activities.
You can create a new Intent that specifies the class to launch, and fire this intent. This will load your other activity.
Something like:
Intent intent = new Intent(this, OtherClass.class);
startActivity(intent);

Assuming you mean start one Activity from another, you would do the following in Activity1:
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);

Related

How to start a Activity of Kotlin from Java android

Can I do this below prossess in android studio?
I have a Kotlin project that I create another activity with
its Java class and I want to start activity of Kotlin with clicking botton in activity of java then it start Kotlin activity
Kotlin is interoperable with Java. Just start the activity using an Intent like you would normally do in Java.
Yes you can start activity from java to Kotlin and vice versa.
from java
startActivity(new Intent(context,DestinationActivity.class))
from kotlin
startActivity(Intent(this, DestinationActivity::class.java))
in java, it is
startActivity(new Intent(currentActivity.this, nextActivity.class);
if you want to send data to the next activity in java
Intent intent = new Intent(MainActivity.this, nextActivity.class);
intent.putExtra("anyName", value);
startActivity(intent);
for kotlin it is
startActivity(Intent(this#MainActivity, nextActivity::class.java)
if you want to send data to the next activity in kotlin
val intent = Intent(this#MainActivity, SecondActivity::class.java)
intent.putExtra("Name", name)
intent.putExtra("Email", email)
intent.putExtra("Phone", phone)
startActivity(intent)

Android Is An Intent Unique To An Activity or Global

I'm curious as to whether an intent is unique to a set activity or not.
For example, my current usage of an intent is to give it some data from Activity 1 and pass it on to Activity 2.
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("id", id.getText().toString());
intent.putExtra("weight", weight.getText().toString());
intent.putExtra("zipTo", zipTo.getText().toString());
intent.putExtra("locationId", locationID.getSelectedItem().toString());
intent.putExtra("imageNum", 1);
startActivity(intent);
Now, in MainActivity I can use the following code to retrieve the data passed from the previous activity.
Bundle bundle = getIntent().getExtras();
id = bundle.getString("id");
weight = bundle.getString("weight");
locationID = bundle.getString("locationID");
zipTo = bundle.getString("zipTo");
After doing some work in MainActivity, I need to return to the previous activity that sent us to MainActivity (and I need the data that was originally sent to return).
Do I need to redefine an intent, and do "intent.putExtra" for each value again in MainActivity before sending it to the first Activity? Or is the intent global, and once defined in one activity I can use it in the others with getIntent().getExtras()?
Intents are not global. You only have access to the bundle in the activity you started with the intent passed.
You should use startActivityForResult(intent). This provides the behavior youre looking for.
Getting a result from an activity
The idea is you start the new intent. In this case 'MainActivity'. Once you are done with your logic in MainActivity you use the strategy above to pass a result back to the activity that started MainActivity.
information passed through intents are not global from one activity to another, what is global is shared preferences and information that is metadata which is stored in manifest.xml.

Use variables between two activities with different processes

I created my Base activity in separated process by adding to it's <activity> fragment in manifest:
android:process=":login_exception_process"
In Base activity I initiate my connection objects, and start next Activitty. But from another Activity when I try to get this instances of connection objects they are NULL.
Objects initiated by Base activity are saved in separated classes in static variables. How can I access this variable?
Passing values to another Activity
You have to start your next activity sending an extra in an Intent.
Intent intent = new Intent(this, NextActivity.class);
intent.putextra("keyName","value");
startActivity(intent);
And your next Activity, you can retrieve this value by doing this:
String data = getIntent().getExtras().getString("keyName");
Here is the documentation for that.
Getting a result from an Activity
Starting another activity doesn't have to be one-way. You can also start another activity and receive a result back. For example, if you want to start the NextActivity and return a value back from NextActivity to BaseActivity, you can use startActivityForResult().
Here is the documentation for that.
Update
Sending objects via intent can be done with two options:
Pseudo-codes
Serializable
intent.putExtra("MyClass", your_object);
// to retrieve object in second Activity
getIntent().getSerializableExtra("MyClass");
Parcelable
Intent mIntent = new Intent(this, NextActivity.class);
Bundle mBundle = new Bundle();
mBundle.putParcelable(PAR_KEY, your_object);
mIntent.putExtras(mBundle);
startActivity(mIntent);
Despite Serializable seems to be the easiest option, follow this tutorial it will teach you both ways.
Hope this helps you.
You should use Extras
How do I get extra data from intent on Android?
Basically, when creating your intent you have to put extras:
intent.putExtra("name", "My name is John");
then when you want to get it back when you start the next activity:
this.getIntent().getExtras().getString("name");
You can do this with any type if you convert the Object to a byte array.

Use the same object between activities

I have tried a lot of different ways to use the same object in different classes. I want to do this because I want the data to be stored in this object.
But no matter what I do, it hust wont be stored when I press the "back" button on the phone and then go back.
I am sending a bundle with the object to the second activity like this:
Intent intent = new Intent(this, TrainingDays.class);
Bundle bundle = new Bundle();
bundle.putSerializable("programObject", program);
intent.putExtras(bundle);
startActivity(intent);
And I am receiving the object like this:
Bundle bundle = this.getIntent().getExtras();
program = (Program) bundle.getSerializable("programObject");
I can use the object just fine, but I every time I switch back to another activity, the object is clean from data. Am I doing something wrong here or could it be something else that causes this? Any help will be greatly appreciated.
That's the correct way to manage objects. If you do not need to write the object on a File I would suggest to switch to Parcelable. About your problem, if you want to pass back the object, you can use the pair startActivityForResult and onActivityResult
Override the onBackPressed() method of Activity and use #blackbelt 's combination of startActivityForResult() and onActivityResult()
You can see an example on the developer page http://developer.android.com/reference/android/app/Activity.html#StartingActivities
onBackPressed() controls going back to the previous activity you could create a new Intent in the onBackPressed(), then a create a Bundle and Put the Bundle in the setResult() method as such setResult(RESULT_OK, mBundle); and Bundle Object will get passed back the previous activity.
in Short
#Override
public void onBackPressed(){
Intent data = new Intent();
Bundle newBundle = new Bundle();
newBundle.putParcelable("MyObject", mObject);// must extend Parcelable on Object class
data.putBundleExtra("ObjectBundle", newBundle);
setResult(RESULT_OK, data);
finish();
}

intent flag over intent startActivity(i);

#Override
public void onClick(View view) {
// Launching News Feed Screen
Intent i = new Intent(getApplicationContext(), Profile.class);
startActivity(i);
}
});
what is the difference of using this code and what difference does it do on the program compared to this doe
Intent i = new Intent(CurrentActivityName.this, NextActivityName.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
First one uses getApplicationContext() to launch the intent.
Application context is attached to the application's life-cycle and will always be same throughout the life of application. So if you are using Toast, you can use application context or even activity context (both) because a toast can be raised from anywhere with in your application and is not attached to a window.
Second one uses the Activity context.
Activity context is attached to the Activity's life-cycle and can be destroyed if the activity's onDestroy is raised. If you want to launch a new activity, you must need to use activity's context in its Intent so that the new launching activity is connected to the current activity (in terms of activity stack).
So, Whenever you need to manipulate Views then go for Activity-Context, else Application-Context would be enough.
Source: this answer at difference and when to use getApplication(), getApplicationContext(), getBaseContext() and someClass.this question.
Also, reading some of above links would help:
What's the difference between the various methods to get a Context?
getApplicationContext(), getBaseContext(), getApplication(), getParent()

Categories