on back button previous activity won't get updated with new data - android

I have two activities profile_Activity and Edit_Profile_activity when I am moving from Profile_Activity to Edit_Profile_activity and doing some changes on profile and update the profile, Activity updates successfully but updated values are not shown on profile_Activity when the back button of mobile is pressed. also the value is shown on press UpEnabled button.

start activity by using
Intent intent = new Intent(mActivity, Edit_Profile_activity.class);
startActivityForResult(intent, REQUEST_CODE);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_CODE:
if (resultCode == Activity.RESULT_OK && data!=null) {
String abc=data.getStringExtra("data_key") ;
//write your code for update info
}
break;
}
}
In your Edit_Profile_activity return result by using
Intent intent=new Intent();
intent.putExtra("data_key",value);
setResult(Activity.RESULT_OK, intent);
finish();

Related

The effect of actions in one activity on another in android studio

In android studio I have two activities, Main and Settings.
The settings activity is called from main activity and there is a button for logging out. When I click on this button the settings activity finishes and the main activity appears again. How can I make the main activity know that the log out button was clicked?
And what if there is many actions that I can perform in settings activity?I don't want to return result to main activity, I want to write that actions somewhere and read from there in Main activity.
In the MainActivity you can start the SettingActivity using
Intent intent = new Intent(this, SettingActivity .class);
startActivityForResult(intent, yourRequestCode);
after that in SettingActivity when you click the button logout, parse the boolean using
Intent intent= new Intent();
intent.putExtra("isClicked", true); // save clicked data
setResult(Activity.RESULT_OK, intent);
finish();
then, in MainActivity you can call onActivityResult like this
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == yourRequestCode) {
Boolean isLogoutClicked = data.getExtras().getBoolean("isClicked");
}
now you get the data from the setting activity when the button logout is clicked.
in mainActivity
Intent intent = new Intent(this, SettingActivity .class);
startActivityForResult(intent, 123);
than
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
try {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 123 && resultCode == RESULT_OK)
{
String requiredValue = data.getStringExtra("key");
}
} catch (Exception ex) {
Toast.makeText(Activity.this, ex.toString(),
Toast.LENGTH_SHORT).show();
}
}
and set thus into your setting screen
Intent intent = getIntent();
intent.putExtra("key", value);
setResult(RESULT_OK, intent);
finish();
and check if value have data or anything you past that its come from setting screen

OnActivityResult get null as the intent passed even though I sent an intent

I have two activities. The second activity is called from the first using startActivityForResult(intent,request_code).
In the second activity I have this code:
Intent i = getIntent();
i.putExtra("data" , some data);
setResult(Activity.RESULT_OK,i);
finish();
Then, to get the data in the first activity I use this code:
#Override
protected void onActivityResult(int requestCode,int resultCode,Intent intent)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==this.uploadRequestCode && resultCode == Activity.RESULT_OK)
{
data = intent.get("data")
}
I start the activity this way:
Intent uploadIntent = new Intent(this,uploadActivity.class)
startActivityForResult(uploadIntent,this.uploadRequestCode)
The problem is that the result code I get is RESULT_CANCELLED even though I set RESULT_OK.
NOTE
I am not talking about a situation when back button is pressed.
UPDATE
I found out that the intent I get in onActivityResult() is null even though I sent an intent, that's why the result code was RESULT_CANCELLED.
Anyone knows why the intent is null?
Maybe this can help you out, something from a project that I have
This how I do start my activity
Intent intent = new Intent(FirstActivity.this, ActivityThatReturnInfo.class);
startActivityForResult(intent, 1);
This is from the activity that I want to return some value
Intent returnIntent = new Intent();
returnIntent.putExtra("VAR",someInfo);
setResult(Activity.RESULT_OK,returnIntent);
finish();
This is on the activity that as started the one that is waiting for the information
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch (requestCode)
{
case 1:
if(resultCode == Activity.RESULT_OK)
{
String result = data.getStringExtra("VAR");
// Code to do
}
break;
default:
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null)
{
if(result.getContents() != null)
{
// Code to do
}
}
break;
}
}
Starting SecondActivity from FirstActivity
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 100);
Sending data to FirstActivity from SecondActivity
Intent intent= new Intent();
intent.putExtra("result",result);
setResult(RESULT_OK,intent);
finish();
getting result in FirstActivity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == 100 && resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("result");
Log.e("Result",result);
}
}
Edit:
Change your code to below and try once.
While sending intent from SecondActivity to FirstActivity Intent i = getIntent(); to Intent intent= new Intent();

Get result from activity called with Intent [duplicate]

This question already has answers here:
How to manage startActivityForResult on Android
(14 answers)
Closed 8 years ago.
I am new to android development
I call an intent now how can i get result from called activity
can any one tell me how to perform this task ?
i have called intent like.
Intent I = new Intent (this ,abc.class);
startActivity(i);
thanks
Use startActivityForResult and then override onActivityResult in your FirstActivity.
In FirstActivity
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
startActivityForResult(intent, 2);// Activity is started with requestCode 2
Override onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
String message=data.getStringExtra("MESSAGE");
Log.i("Message is",message);
// logs Testing
}
}
In SecondAcivity
Intent intent=new Intent();
intent.putExtra("MESSAGE","Testing");
setResult(2,intent);
finish();//finishing activity
Reference to the docs:
http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)
Example:
http://www.javatpoint.com/android-startactivityforresult-example
For going to second activity use startActivityForResult in your firstclass
Intent callIntent = new Intent(FirstClass.this, SecondClass.class);
startActivityForResult(callIntent, 1);
then override onActivityResult method in your first class like this
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == 1) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// get values from data
}
} }
In your second class do this for returning back, if you want to send
something to first class. Store this in your intent.
Intent result = new Intent(); setResult(Activity.RESULT_OK, result);
finish();
hi you can get result calling activity
Start activity with startActivityForResult(intent, 0);
add below method in calling activity
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
// do your task
} else if (resultCode == RESULT_CANCELED) {
// do your task
}
}
}
In your main Class....
static final int CODE_REQUEST = 1; // The request code
....
Intent pickContactIntent = new Intent(MainClass.this, CallingClassName.class);
startActivityForResult(pickContactIntent, CODE_REQUEST);
..........
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The user picked a contact.
// The Intent's data Uri identifies which contact was selected.
// Do something with the contact here (bigger example below)
}
}
}
In your calling class
Intent result = new Intent();
setResult(Activity.RESULT_OK, result);
finish()

how to get back some text from a calling activity

I have an activity that Call another activity for filling an address and second activity should send back the address for the first activity and show it in a textview in first activity
i used these codes
but i dont know why it s not working
first activity:
Intent in = new Intent(getApplicationContext(),ShippingActivity.class);
startActivity(in);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RESULT_OK){
tvDeliverTo.setText(data.getStringExtra("DeliveryAdressKEY"));
}
}
and second activity
Intent in = new Intent();
in.putExtra("DeliveryAdressKEY", tvAdress.getText().toString());
setResult(RESULT_OK, in);
finish();
thanks in advance
You need to start the second activity with startActivityForResult:
http://developer.android.com/reference/android/app/Activity.html#startActivityForResult%28android.content.Intent,%20int%29
In onActivityResult, you should also check the resultCode is RESULT_OK, rather than the requestCode.
I believe you should be using startActivityForResult() instead of startActivity().
first activity
Intent in = new Intent(getApplicationContext(),ShippingActivity.class);
startActivityForResult(in, 0);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 0){
if(resultCode ==1){
tvDeliverTo.setText(data.getStringExtra("DeliveryAdressKEY"));
}
}
}
and second activity
Intent in = new Intent();
in.putExtra("DeliveryAdressKEY", tvAdress.getText().toString());
setResult(1, in);
finish();
and now is working

Handling onActivityResult in Android app having more than one activity

In my android app, I have a main activity which creates two other sub activites through intent. Now, both the sub activity return result to the main activity. In my main activity,
how do I handle two "onActivityResult(int requestCode, int resultCode, Intent data)" since it cant have two methods with same name in a given class. Hope my question is clear..
Thanks
You change the requestCode that you use when you call startActivityForResult.
EDIT: for example, I use this:
startActivityForResult(i, App.REQUEST_ENABLE_BT);
and this:
startActivityForResult(i, App.MANUAL_INPUT);
and then you filter the results like this:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
switch(requestCode){
case App.REQUEST_ENABLE_BT:
if(resultCode != RESULT_OK){
Toast.makeText(this, getString(R.string.label_bluetooth_disabled), Toast.LENGTH_LONG).show();
}
break;
case App.MANUAL_INPUT:
break;
}
}
That's what the requestCode is for. So you'd have a setup like this
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode)
case ACTIVITY1:
if(resultCode == RESULT_OK)
Toast.makeText(getApplicationContext(), "Activity 1 returned OK", Toast.LENGTH_LONG).show();
break;
case ACTIVITY2:
if(resultCode == RESULT_OK)
Toast.makeText(getApplicationContext(), "Activity 2 returned OK", Toast.LENGTH_LONG).show();
break;
}
Where ACTIVITY1 and ACTIVITY2 are constants in your Activity. You'd call them like so:
startActivityForResult(activity1Intent, ACTIVITY1);
and
startActivityForResult(activity2Intent, ACTIVITY2);
It's possible to return any kind of data from a subactivity in the result intent parameter:
Sub-activity:
Intent intent = new Intent ();
intent.putExtra ("string_1", "hello");
intent.putExtra ("string_2", "world");
intent.putExtra ("int_1", 1000);
intent.putExtra ("long_1", 2000l);
activity.setResult (Activity.RESULT_OK, intent);
_
Parent activity:
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent intent)
{
if (resultCode == Activity.RESULT_OK)
{
String string_1 = intent.getStringExtra ("string_1", "");
String string_2 = intent.getStringExtra ("string_2", "");
int int_1 = intent.getIntExtra ("int_1", 0);
long long_1 = intent.getLongExtra ("long_1", 0);
}
}
You can use swicth the requestcode for different result
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case (1):
{
// do this if request code is 1.
}
break;
case (2):
{
// do this if request code is 2.
}
break;
}

Categories

Resources