Im new on Java so its confusing me a little bit, i want to create an intent for Activity two but there seem to be a problem with the code that i have written.
#Override
public void onClick(View v) {
// TODO:
// Launch Activity Two
// Hint: use Context's startActivity() method
// Create an intent stating which Activity you would like to
// start
Intent activityTwo = new Intent(ActivityTwo.this.finish());
Intent intent = null;
// Launch the Activity using the intent
startActivity(activityTwo);
}
});
// Has previous state been saved?
if (savedInstanceState != null) {
// TODO:
// Restore value of counters from saved state
super.onRestoreInstanceState(savedInstanceState);
mCreate = savedInstanceState.getInt(CREATE_KEY);
mRestart = savedInstanceState.getInt(RESTART_KEY);
mStart = savedInstanceState.getInt(START_KEY);
}
// Emit LogCat message
Log.i(TAG, "Entered the onCreate() method");
// TODO:
from First to Second:
Button next = (Button) findViewById(R.id.button2);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),Second.class);
intent.putExtra("Tag", "Value");
startActivity(intent);
finish();
}});
Second to First:
Button previous= (Button) findViewById(R.id.button);
previous.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),First.class);
startActivity(intent);
}});
First Understand the concept of Intent.
In your case, you want to call ActivityTwo from ActivityOne.
follow this steps
Create ActivityTwo.
Declare ActivityTwo in your AndroidManifest.xml
<activity android:name=".ActivityTwo" />
Write Intent code where in onclick() method.
Intent intent = new Intent(getApplicationContext(),ActivityTwo.class);
startActivity(intent);
In this code Intent Constructor contains two parameters.
Current Context
ActivityTwo reference.
Related
Here's my code for going to an activity from a fragment
Intent intent = new Intent(getActivity(), Product_info.class);
intent.putExtra("product_key", postIDs.get(position));
startActivity(intent);
And here's my code for going back to that fragment from that activity
back_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
For context, I am changing some details displayed on the fragment on the activity. I have tried to use intent instead of finish but it doesn't work as intended. Is there a way to refresh the fragment before going back to it?
If you want to get some results from activity and refresh status in fragement then use start activity for result. and before finishing your activity set result ok or cancel and check for result in fragment
example
ActivityResultLauncher<Intent> launchSomeActivity = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
#Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
// update your fragment here
}
}
});
public void openYourActivity() {
Intent intent = new Intent(this, SomeActivity.class);
launchSomeActivity.launch(intent);
}
once you have done some action in activity then you need to set result to OK or cancel before finishing activity
if(some condition) {
//return data if you want to resurn to calling activity or fragment
Intent intent = new Intent()
intent.putExtra("data", data)
setResult(Activity.RESULT_OK, intent)
}
finish()
I have Activity1, Activity1 Adapter and Activity2
I'm not able to pass value between an Adapter and Activity. When back button is pressed, I'm expecting a value to be coming from the Second Activity (Activity 2) to Activity1 Currently, it gives me null
Here are my code snippets.
Activity1 Adapter
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Activity origin = (Activity) context;
Intent intent = new Intent(context, PostActivity.class);
intent.putExtra("searchText", staggeredCustomCard.getSearchText());
origin.startActivityForResult(intent, 1);
}
});
Activity2
#Override
public void onBackPressed() {
super.onBackPressed();
Intent mIntent = new Intent();
mIntent.getStringExtra("searchText");
setResult(1, mIntent);
}
I'm expecting this searchText to be going to Activity1. Could anyone please guide me how to achieve this?
You are getting getStringExtra("searchText"); from a completely new intent, that's why it's returning null. You need to get search text from getIntent() like this:
#Override
public void onBackPressed() {
super.onBackPressed();
Intent mIntent = new Intent();
String search = getIntent().getStringExtra("searchText");
mIntent.putExtra("searchText", search);
setResult(1, mIntent);
}
I have in my firstrun activity, a button that says "continue" and starts new activity (MainActivity intent), but I've found out it's possible skip that button and firstrun activity by restarting the app. So I would like to make sure it will be impossible to skip the activity.
That's what I have been thinking about:
firstEntry.java - In this method I'm putting new value in the shared preference that confirm that firstrun haven't been skipped (it only add the value if the button is clicked).
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean(skip, false);
editor.commit();
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
finish();
}
OnCreate() method in MainActivity
boolean skipper = prefs.getBoolean(skip, false);
if(skipper == true){
Intent i = new Intent(getApplicationContext(), firstEntry.class);
startActivity(i);
}
OnResume function:
#Override
protected void onResume() {
super.onResume();
if (prefs.getBoolean("firstrun", true)) {// Checks if application is on its first run
Intent i = new Intent(getApplicationContext(), firstEntry.class);
startActivityForResult(i, 1);
prefs.edit().putBoolean("firstrun", false).commit();
finish();
}
}
How can I check if shared preference method "firstrun" has skipped the continue button by using shared preference values?
change following line in btn.setOnClickListener(new View.OnClickListener()
editor.putBoolean(skip, false);
to
editor.putBoolean(skip, true);
And then in OnCreate() method of firstEntry.java
boolean skipper = prefs.getBoolean(skip, false);
if(skipper == true){
Intent i = new Intent(getApplicationContext(), MainActivity.class);
Log.i("firstEntry.java","skipped the activity");
startActivity(i);
}
I have one activity which is common for all other activities. I want to call this activity and want to set some conditions based on from which activity it has been called. I thought of bundle for this. How can I call a condition based on bundle value? I have another activity in between. I am not calling the activity directly. So how can we pass data by using bundle?
txt_from.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i = new Intent(getApplicationContext(), PickLocationActivity.class);
GoSendData.instance.addressType = 0;
i.putExtra("type",1);
startActivity(i);
}
});
From this I am calling second activity.
In common activity I have a view I am calling the activity back from this. The activity from which it has been called , it should be called back from this view.
useLocation.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Bundle extras = intent.getExtras();
activityType = extras.getInt("type");
if(activityType==0) {
intent = new Intent(ChooseFromMapActivity.this, GoSend.class);
startActivity(intent);
}
if(activityType == 1)
{
intent = new Intent(ChooseFromMapActivity.this, GoRideActivity.class);
startActivity(intent);
}
}
});
How to achieve this...?
How can I do this with shared preferences?
Change
Bundle extras = intent.getExtras();
to
Bundle extras = getIntent().getExtras();
Hope this helps :)
Check the below code
Activity_1. : This will send the data to the Common Activity.
Intent i = new Intent(Activity_1.this, CommonActivity.class);
i.putExtra("type",1);
startActivity(i);
Activity_2. : This will send the data to the Common Activity.
Intent i = new Intent(Activity_2.this, CommonActivity.class);
i.putExtra("type",2);
startActivity(i);
Then on your Common Activity write this code in the onCreate function.
int receivedValue = getIntent().getIntExtra("type", 0);
// here 0 is the default value when there is no data in the particular key.
now you can check the condition like this
if(receivedValue==1)
{
// do something here
}
if(receivedValue==2)
{
// do something here
}
This will definitely work for you. Try it..!!
Happy coding.
Activity1,
txt_from.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i = new Intent(getApplicationContext(), PickLocationActivity.class);
GoSendData.instance.addressType = 0;
i.putExtra("type",1);
startActivity(i);
}
});
Activity2,
txt_from.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i = new Intent(getApplicationContext(), PickLocationActivity.class);
GoSendData.instance.addressType = 0;
i.putExtra("type",2);
startActivity(i);
}
});
Common Activity
useLocation.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = getIntent();
int type = i.getInt("type");
if(type==1) {
Intent intent = new Intent(ChooseFromMapActivity.this, Activity1.class);
startActivity(intent);
}
if(type == 2)
{
Intent intent = new Intent(ChooseFromMapActivity.this, Activity2.class);
startActivity(intent);
}
}
});
i'm having an issue with Intents and putExtra.
What i want to do is this :
In Activity A(it's not my MainActivity),when i click a button,it will close all my activities, send a string and launch my main activity.For testing purposes it will show a test dialog with my string.All good till now,works as i need it to.
The problem is that if i restart my MainActivity(and i need to do that,it's something like a shopping list,i need to start a new shopping list) the dialog with the putExtra string shows again.
Here are my code snippets :
In Activity A :
#Override
public void onClick(View v) {
Intent intent = new Intent(Gestionarez.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent.putExtra( "paramName", str );
startActivity( intent );
// TODO Auto-generated method stub
dialog.dismiss();
dialog.cancel();
}
In my MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LoadPref();
Bundle extras = getIntent().getExtras();
if (extras != null) {
String myParam = extras.getString("paramName");
ShowAlertMessage(this, "TEST", myParam + "");
} else {
}
}
And this is how i restart my MainActivity when i need to start a new shopping list :
Intent intent = getIntent();
finish();
startActivity(intent);
replace
Intent intent = getIntent();
finish();
startActivity(intent);
with
Intent intent = new Intent(this, ActivityB.class);
finish();
startActivity(intent);