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)
Related
This question already has answers here:
Android: Go back to previous activity
(24 answers)
Closed 3 years ago.
I have two activities, How is it possible to go back to a previous activity.
What code do I need to go back to previous activity
Assuming you started the new activity with
startActivity(new Intent(this, SecondActivity.class));
puts the SecondActivity in front and the FirstActivity in the backstack. To go back to the FirstActivity, put this in your second activity.
finish();
If you start the activity with result like below :
From 1st Activity :
startActivityForResult(new Intent(this, SecondActivity.class),requestCode);
You can finish the activity with required intents :
From 2nd Activity :
// Optional if you want to pass values from second activity to first
Intent data = new Intent();
data.putExtra("key","any_value");
setResult(RESULT_OK,data);
// Just finish
finish();
Refer the below link for more information like onActivityResult callback and more
https://developer.android.com/training/basics/intents/result
1st Method
If you are using Intent then make sure you don't call finish(); method after using the following code,
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
By this code, the Back Button you created will do its work and go to previous Activity.
And also don't override the onBackPressed() method in Activity.
2nd Method
There is another way that you can achieve this by setting Home Button to go to specific Activity, For that, you have to create Back Button in Action Bar in onCreate() method.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
And in the AndroidManifest.xml file, you have to add the following,
<activity
android:name="com.example.yourapplication.SecondActivity"
android:label="#string/title_second_activity"
android:parentActivityName="com.example.yourapplication.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.yourapplication.MainActivity" />
</activity>
using intent
intent = new Intent(this, SecondActivity.class);
startActivity(intent);
Or you can use onBackPressed() if the activity added to backstack
onBackPressed();
first I will give a brief explanation about my program.
I am trying to build a Point Of Sales System in Android Studio, Currently I have 4 Activities which are Login Activities, Register Activities, Forgot Password Activities, and Main Activities.
Basically when you use Finish() in each activities, it will dismiss all operation on the activity, in other words you can't go back to the previous activity after the Intent.
I am wondering instead to put Finish() on each activities, can I do that to all my activities automatically?
You can do this by keeping no histroy of back stack. Try below code.
Intent i = new Intent("your intent stuff");
i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i)
or second way of doing the same You can do this by adding flag in your manifest as well. Add below code in your manifest file.
android:noHistory="true"
You need to add above tag in application tag.
Hope this will help you.
Just add flags into the intent before starting activity
in Kotlin
val intent = Intent(this, NewActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
startActivity(intent)
in java
startActivity(new Intent(this, NewActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
You can add this.And it's clear all previous activity and data.
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
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.
I create a fresh project in Android studio using the wizard that creates a blank activity with a fragment. I then create a second basic activity with a fragment. In the first fragment I add a button, which has an OnClickListener, and inside of onClick() it does the following:
Intent intent = new Intent(getActivity(), SecondActivity.class);
getActivity().startService(intent);
When I click the button, despite the code being run, the activity is never launched. There are no errors in logcat. Because I did it with the wizard, the activity is automatically added to the manifest.
Your code calls startService() but it should startActivity():
Intent intent = new Intent(getActivity(), SecondActivity.class);
getActivity().startActivity(intent);
Use startActivity() insteed of startService()
Intent intent = new Intent(getActivity(), SecondActivity.class);
getActivity().startActivity(intent);
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);