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);
Related
Is it possible to relaunch an activity A from activity B after the finish() method has been called from activity A?
how can I go about it in order to achieve this?
Intent intent = new Intent(this, Activityb.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Use this piece of code and just change the activity name accordingly. This will finish your acitivty A and redirect to activity B
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)
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();
I tried to set attribute android:launchMode="singleTask" for calling Activity, but it still does not works as I expected.
I need that method onCreate(Bundle bundle) to be called only once, but it still calls each time when I start Activity.
I start Activity using code like this:
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), NextActivity.class);
startActivity(myIntent);
}
Please let me know what I am doing wrong
It must be like this:
android:launchMode="singleTop"
and calling:
Intent intent= new Intent(context, YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Already existed topic about this: Android: new Intent() starts new instance with android:launchMode="singleTop"
Even if you make your launch mode 'singleTop', a new activity will be started.
At each activity creation, its onCreate() will be started.
My understanding is that the singleTop option, basically finishes the caller Activity.
I think that you may think that onCreate() is a form of Application constructor but it really is an Activity constructor. You may want to do your one time initializations elsewhere.
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);