Is it possible to pass a value from Adapter to Activity? - java

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);
}

Related

How to refresh a fragment before going back to it from an activity?

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()

Android Studio: Activity only finishes when you call the finish() for second time

So, we're working with intents at school and I'm having trouble with the intents when I try to pass data from the "Activity2" to the "Activity1", when I do the setResult() and stuff. The problem is it won't go back to the first activity when I trigger the event the first time, but it will the second.
I've been working with Android studio only for about 12h so I really lack a lot of understanding.
Here is what I'm doing:
First I call this form the main activity.
public void CheckPassword(View view) {
password = PasswordManagement.getPassword(this);
TextView txtPassword = findViewById(R.id.txtPassword);
if (txtPassword.getText().toString().equals(password)) {
Intent intent;
intent = new Intent(this, WelcomeActivity.class);
intent.putExtra("password", password);
startActivityForResult(intent, 1);
startActivity(intent);
} else {
Intent intent;
intent = new Intent(this, RestrictedActivity.class);
startActivityForResult(intent, 1);
startActivity(intent);
}
}
Then, when I'm done from the second activity I run this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restricted);
lblRestrictedArea = findViewById(R.id.lblRestrictedArea);
lblRestrictedArea.setOnLongClickListener(
new OnLongClickListener() {
public boolean onLongClick(View view) {
intent = new Intent();
intent.putExtra(EXTRA_RESPONSE, true);
setResult(RESULT_OK, intent);
finish();
return false;
}
});
}
And back to the main activity I overwrote this to act according to the response:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
if (data.getBooleanExtra(RestrictedActivity.EXTRA_RESPONSE,false)){
LinearLayoutPasswordActivity.setBackgroundColor(getResources().getColor(R.color.red));
}else{
LinearLayoutPasswordActivity.setBackgroundColor(getResources().getColor(R.color.white));
}
}
}
}
If anyone can help I would be very glad, meanwhile I'll try to solve it my own.
Thanks!
Your are calling startActivity twice. So there are two instance of the same Activity and then you have to finish twice.
Keep your startActivityForResult(...) and delete startActivity in CheckPassword(View view)
->
public void CheckPassword(View view) {
password = PasswordManagement.getPassword(this);
TextView txtPassword = findViewById(R.id.txtPassword);
if (txtPassword.getText().toString().equals(password)) {
Intent intent;
intent = new Intent(this, WelcomeActivity.class);
intent.putExtra("password", password);
startActivityForResult(intent, 1);
// startActivity(intent);
} else {
Intent intent;
intent = new Intent(this, RestrictedActivity.class);
startActivityForResult(intent, 1);
//startActivity(intent);
}
}
Plus, note that you are using the same requestCode (1) for two different activities. The requestCode is very important for onActivityResult method.

How to call one activity multiple times based on from which the activity has been called?

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);
}
}
});

How Create intent for an activityTwo

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.

Intent putExtra persists

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);

Categories