Sending data to Activity without startActivityForResult - android

How can I send data to another Activity that did not startActivityForResult?
I Override the onActivityResult method in order to perform logic as soon as a result returns from another Activity. Specifically, from my EditItemActivity
Process:
Within my MainActivity,
when I click on an item in the ListView, I am redirected to another Activity called ToDoDetailActivity
When I click the Edit button I am redirector to another Activity called EditItemActivity"
As soon as I make my changes and press the Edit button on the EditItemActivity, I return back to the MainActivity
data = new Intent(EditItemActivity.this, MainActivity.class);
data.putExtra(EDITTEXT_VALUE, etValue);
setResult(123, data);
startActivity(data);
Within my MainActivity, I want to UPDATE the item I just edited with this logic within my onActivityResult method. However, I don't see any logs in LogCat meaning I do not believe this method is being used and therefore no logic is performed
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data == null) {
return;
}
if (resultCode == 123) {
String editedItemValue = data.getStringExtra(EditItemActivity.EDITTEXT_VALUE);
todoItems.remove(editedPosition);
aToDoAdapter.insert(editedItemValue, editedPosition);
aToDoAdapter.notifyDataSetChanged();
}
}

In your code:
data = new Intent(EditItemActivity.this, MainActivity.class);
data.putExtra(EDITTEXT_VALUE, etValue);
setResult(123, data);
startActivity(data);
When you execute startActivity with MainActivity.class it is launching a new Activity so you are not going to find a result in "onActivityResult" method. It is because you are not returning to the main activity after finishing in a "child" activity.
What I have done in the past is use broadcast event to notify the "MainActivity" about updating the view.
Hope that helps.

We can use BroadcastReceiver or use database to store the result of your edit in EditItemActivity then update that result in onResume method of MainActivity

Related

How to transition from activity 3rd to 1st?

I have a problem with Shared Element Activity Transition between activities. I have MainActivity, it has a recyclerview with boxes (recyclerview.horizontal). Each box when clicked will go to the corresponding activity. The problem that appears when I click on a box, I switch to the second activity, in the second activity I press a button to switch to the third activity. And here I swipe to right to return to the MainActivity with transition and I want it to transition right to the box corresponding to the 3rd activity in the recyclerview in MainActivity. So, my purpose is:
MainActivity (Shared Element Activity Transition)-> Second Activity ->
Third Activity (Shared Element Activity Transition)-> MainActivity
(exactly scroll to position for Third Activity in RecyclerView).
My MainActivity
I hope everyone offer me the solution. Thank you so much.
You can use startActivityForResult instead of startActivity in SecondActivity when you are going to start ThirdActivity.Like this :
Intent i = new Intent(this, ThirdActivity.class);
startActivityForResult(i, 1);
And when you are finishing your ThirdActivity
Intent returnIntent = new Intent();
returnIntent.putExtra("activity_finish",true);
setResult(Activity.RESULT_OK,returnIntent);
finish();
If you use startActivityForResult() then it gives callback in the Activity who started it,so as soon as ThirdActivity finishes, it will return to the onActvityResult() in SecondActivity.Where you have to check result_code and request code like this :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
boolean isActivityFinish=data.getBooleanExtra("activity_finish");
if(isActivityFinish){
// finish your Second Activity here
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
For more info : How to manage `startActivityForResult` on Android?

How check if my previous activity exist?

In my app, there are 3 activities and their calling sequence is like this...
Splash activity--->Dashboard Activity--->List Activity
If it goes like this then If I press back button on List activity then it will follow the reverse sequence of above.
There is one requirement where user can navigate to List Activity directly from Splash (Skipping Dashboard Activity) Now when user will hit back button on List Activity then I wan't to show Dashboard Activity which is not there in the Activity Stack.
So please help me with the best approach.
Pass a boolean through the intent for going to List Activity from either of the others. Using onBackPressed check if the boolean is true or false for skipping Dashboard Activity.
Then if true put new intent for loading dashboard activity and finish(); on list activity.
You have to pass class name as intent extra from both Splash and DashboardActiviy.
In List Activity you have to get the class name using getIntent().
When the user click back button, you need to check the class name based on that you can take decision.
if(name.equalIgnorecase(DashboardActivit.class.getSimpleName()){
//Add your intent
}else{
//
}
This may give you definite solution to you.Give a try
you can directly go to splash from list Activity while going to
ListActivity from Dashboard Activity call finish()
Intent i = new Intent(DashboardActivity.this,ListActivity);
startActivtiy(i);
finish();
Start your inner activities with startAcvitiyForResult
Intent i = new Intent(this, Activity_2.class);
startActivityForResult(i, 1);
and in your inner activity
#Override
public void onBackPressed ()
{
finish();
}
You can also do stuff in your outer activity as you like after your inner activity finishes
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == 1){
Log.i("TEST", "RESULT_OK");
}
else {
return;
}
}
}

How to restart previous activity in android?

In my android app, I have an activity that displays profile information. Then it opens a new activity to make changes to the activity. After you save changes, it closes the edit activity from the edit activity, then goes back to the profile displaying activity, there I need to restart this activity to refresh the data.
Is there a way I can restart the activity that opened the current activity?
There are three ways you can achieve what you want:
Start the profile activity by calling startActivityForResult() and refresh your data in onActivityResult()
Finish() the activity when start the profile activity and then override onBackPress() in the profile activity to start the previous activity and then call finish().
Override onBackPress() in the profile activity to start the previous activity with the flag Intent.FLAG_ACTIVITY_CLEAR_TOP and refresh your data in onNewIntent()
Use Android Activity Result Pattern that communticates parent and child activities:
In your profile activity (parent):
#Override
public void onClick(View v) {
startActivityForResult(
new Intent(this, NameActivity.class),
1); // 1 to identify caller activity.
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
updateProfile();
}
}
In your editor activity (child):
#Override
public void onClick(View v) {
setResult(
RESULT_OK);
finish();
}
You won't have to "restart" Activity A.
You can call startActivityForResult(Intent, int) to start your new Activity, and then receive extras via the onActivityResult(int, int, Intent) call.
http://developer.android.com/reference/android/app/Activity.html#StartingActivities
Alternatively, look into the onResume() method, which is called every time your screen becomes visible. Assuming you persisted some state in Activity B (e.g. a SharedPreference or a database record), in onResume() you can refresh Activity A.
Activity Display;
Activity Modification;
When you startActivity M in your Activity D,don't D.finish();
then, when you M.finish(),you don't need "restart" Activity D,because it is already there.
You have two choices to "refresh the data":
1.startActivityForResult as they said.I won't explain any more.
2.make a cache.
save data in some static variables or sqlite or even sharedPreference.Then in onResume() of Activity D,get those data and show them.
Try this: If A is calling B and when B ends you need to update A, then in A launch B with startActivityForResult(). When B returns, the method onActivityResult() of A will be called. In this method (of A) you can test, if necessary, that some stored values are changed. At this point you do something like:
finish();
startActivity(intent); //start the activity A

OnActivityResult Doesn't work with activityGroup

im my App i use TabHost. and ActivityGroup to load activities under the tab. on my 2nd tab i open activityGroup "TabGroupActivity"... and from here i open a child activity "childActivity2". from the "childActivity2" i want to open an normal activity which has a theme dialog. and when i return from my normal activity i want to run the onActivityResult() in my childActivity2.
But the onActivityResult() in ChildActivity2 is not working.
the code where in childActivity2 to start the normal activity is
data.putInt("doctorId", doctor_id);
Intent createSchedule = new Intent(ScheduleWeekly.this, CreateSchedule.class).putExtras(data);
startActivityForResult(createSchedule, 1);
this is my onActivityResult()
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==Activity.RESULT_OK)
{ Log.e("get","result");
.................
......
}
}
Your problem is same as mine. The problem is that the onActivityResult function won't directly trigger from a child activity in activity group, even your intent is from that child activity.
The solution is divided in three steps.
First, you have to let your parent activity, that is your ActiviyGroup class to call the startActivityForResult function in the position you need to jump out the current activity. In your child activity, when you need to lunch your normal activity, instead call:
startActivityForResult(intent, 0);
You should call:
getParent().startActivityForResult(intent,0);
This will let the ActivityGroup to take care of the call back. In your case, since your have three level nested, you may have to try whether the parent or grandparent should take care of the call back and make proper modification about getParent() part.
Second, after you make the parent class of current activity start the intent, you will need to add onAcivityResult() function into BOTH parent class and the current child class. In current class you just write normal call back handle message as you do now. But in parent class, the onActivityResult() function will catch the call back from the normal activity and deliver the intent to the current class.
Third, This step is for the parent onActivityResult class, in that class, your need:
public void onActivityResult(int requestCode, int resultcode, Intent data)
{
super.onActivityResult(requestCode, resultcode, data);
switch (resultcode)
{
case RESULT_OK:
MyChildActivity CA = (MyChildActivity) getLocalActivityManager().getCurrentActivity();
CA.onActivityResult(requestCode, resultcode, data);
}
}
As you can see, the onActivityResult function in parent ActivityGroup class is just catch the call back, get the child activity you which needs to jump to another activity, and transfer the data to it. You may not need exactly onActivityResult function in your child activity as state in Step 2, but I think this is a better way to do it.
Hope this help!

Interaction between activities

I start an activity from the main one using a Intent:
Intent i = new Intent(getApplicationContext(), InfoChiamata.class);
i.putExtra("codice_cliente", codice_cliente[tv_clicked_id]);
i.putExtra("descrizione_chiamata", descrizione_chiamata[tv_clicked_id]);
startActivity(i);
How can I edit the main activity ui from the activity started whit Intent?
How can I know when i return back from the second activity to the main one? I tried to Override the onResume and onStart method but the application doesn't even start. I tryed to override onRestart method when it get called the application crash.
#Override
protected void onRestart() {
if(call_back == 1)
Toast.makeText(getApplicationContext(), "asd", Toast.LENGTH_LONG).show();
}
call_back variable is set to 1 from the secondary activity, when it is launched.
Thanks, Mattia
try startActivityForResult instead which gives you a callback to your first activity. also don't use the application context, unless absolutely necessary, use the activity context instead. Also when you call certain methods from the activity class and override them like onRestart or onStop or onResume you have to do super.onResume() inside the method first, making sure that the app life cycle is not broken.
Try starting your new Activity with
startActivityForResult(i, 1);
Then, in your main activity, use this code to catch when the user leaves the second activity and returns to the first one:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//your request code will be 1 since you started the
//activity with result 1. Your result code will be detemined
//by if the activity ended properly.
}
It sounds like maybe you want to use startActivityForResult(Context, Intent) to call your second activity. When you do this, your second activity can return a value to the activity that called it, and you can decide what to do from there. When the second activity is finished your first activity will receive a callback. This tells your first activity that the second one is finished and it is passed the result of what happened in the second activity.
It is explained in the Android docs here: http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent,%20int)

Categories

Resources