There are two Activities: ActivityA and ActivityB.
From ActivityA I going to ActivityB. Then, I choose some list item and going back to Activity A(same instance) with selected data. And the most difficult: if I press back now, I should going back to ActivityB(with same instance and with saved View state).
Is it possible to implement? Not necessary via launchMode attribute, perhaps there is another way to manage Activities manually via ActivityManager.
In onActivtyResult of Activity put boolean flag so that you can check that it comes after selecting data from Activity B and in onBackPressed of Activity A start Activity B
like this way
boolean flag = false;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
flag = true;
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
if(flag)
{
//Start Activity B
}
else
{
// finish this activity
}
}
Use startActivityForResult to get the result from activity B which was started by activity A. Activity B returns result when it is finished.
Related
I'm using shared element transition when navigate from activity A to activity B.
My issue - calling of SupportFinishAfterTransition in activity B makes OnActivityResult called after OnTransitionEnd with small interval in activity A. So my UI (ImageView) in activity A is "flashing" because of image source updating with interval.
I need OnActivityResult to be called first to update my UI and then play exit transition.
My code:
Activity A:
protected override void OnCreate(Bundle savedInstanceState)
{
// Some code
Window.SharedElementExitTransition.AddListener(this);
}
This code called firstly:
public void OnTransitionEnd(global::Android.Transitions.Transition transition)
{
}
This code called secondly, but I would like to firstly:
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
}
Activity B:
SetResult(Result.Ok, intent);
SupportFinishAfterTransition();
use onActivityReenter instead of OnActivityResult. onActivityReenter executes before activity displayed, OnActivityResult executes after activity displayed.
#Override
public void onActivityReenter(int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
//do your work here
//example of getting data:
int someData = data.getIntExtra("EXTRA_CODE", -1);
}
}
You have to be aware of these things when you want have a transition after returning from startActivityForResult:
i.) you need to specify a windowSharedElementReenterTransition, usually only windowSharedElementEnterTransition and windowSharedElementEnterTransition are specified.
ii.) in the onActivityReenter of the reentered Activity you need to call postponeEnterTransition(), then update your views as desired and call again startPostponedEnterTransition().
onActivityResult is only called when the previous Activity is finished, whereas onActivityReenter is called when the previous Activity is still running (you can get the Intent there to update the UI).
Here is an article which brings light into the dark:
https://www.androiddesignpatterns.com/2015/03/activity-postponed-shared-element-transitions-part3b.html
I am trying to do the following:
Show dialog fragment from an Activity (not Fragment);
Retrieve result in the Activity.
The problem is that onActivityResult in the Activity is not being called.
Here is how I am showing a DialogFragment
DriverRatingDialogFragment dp = new DriverRatingDialogFragment();
// this solution works well in case of showing dialog from a fragment
// but I have to show it from the Activity
// dp.setTargetFragment(getSupportFragmentManager().findFragmentById(R.id.flContent), DriverRatingDialogFragment.REQUEST_CODE);
dp.show(getSupportFragmentManager(), DriverRatingDialogFragment.ARG_RATING);
And here is how I am trying to return result from DialogFragment:
Intent intent = new Intent();
// This solution works well in case of previously setting target Fragment,
// but I have to return result to Activity
// getTargetFragment().onActivityResult(
// getTargetRequestCode(), REQUEST_CODE, intent);
// and with this attempt App crashes
// this.onActivityResult(REQUEST_CODE, Activity.RESULT_OK, intent);
here is where I want to retrieve result in the Activity, but it doesn't get called:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == DriverRatingDialogFragment.REQUEST_CODE) {
// todo
}
super.onActivityResult(requestCode, resultCode, data);
}
What's wrong?
I start the DialogFragment the same way you do.
CreatePostDialog createPostDialog = new CreatePostDialog();
createPostDialog.show(getSupportFragmentManager(), "create_post_dialog");
And I send back data using a public method in the activity rather than with intents.
((MainActivity)mContext).logout(Utils.getLocation(v));
The snippets above are copy pasted examples of my working code. Logout in this case is a public method in MainActivity that takes in a point and does what is needs with it.
you can use callback
1.define a callback in you dialog fragment
public Call mCall;
public interface Call {
void returnData(Data data);
}
2.on dialog fragment life circle
#Override
public void onDetach() {
super.onDetach();
mCall = (Call) getActivity();
mCall.returnData(data);
}
3.activity just implements Call
activity implements DiaFragment.Call
onActivityResult is there to receive informations after startActivityForResult is called, which is not your case.
What kind of datas you need to get from the dialog ?
DialogFragment is usually used in a fragment, not an activity. Look at alert dialog.
Can you please clarify me, what's the best practice for this example:
Activity1 does INSERTS on SQLite and starts Activity2.
On Activity2, the user clicks on Back button and goes back to Activity1.
But instead of making INSERT, I want to make UPDATE to the data on SQLite.
How do I control, that user "came" back?
What's the best practice?
Assume on button click you starting Activity 2
public static final int YOURCONSTANTNUMBER = 77;
//Button Value
Button second = (Button)findViewById(R.id.firstButton);
second.setOnClickListner(this);
#Override
public void onClick(View v)
{
switch(v.getId())
{
case R.id.btnSecondActivity:
//Code for inserting data into database
Intent secondactivity = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(secondactivity);
break;
}
}
In your second activity
To take control over the back button use the below code
#Override
public void onBackPressed()
{
//Code for update
}
EDIT
Call your second activity with startActivityForResult() method which will let you know the result.
In First Activity
Intent secondactivity = new Intent(FirstActivity.this, SecondActivity.class);
startActivityForResult(secondactivity,YOURCONSTANTNUMBER);
Getting Result
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode)
{
case YOURCONSTANTNUMBER:
// It will execute if you returning from second activity
//Update you database here
}
}
In Your Second Activity
Call this method were appropriate setResult(YOURCONSTANTNUMBER);
Hope it helps
Assuming you want to do both DB operations in Activity1, consider startActivityForResult() here:
http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)
I have one activity A, that has one button and one list view which shows names of books . on click of the button, activity B starts, there user fill the book form and save it . when he press back button , user comes to activity A. Here the book name should be updated in listview. I think I have to write some code in onResume() . Can u please tell me what to write. I am using customised list view.
Start activity B with startActivityForResult() and use method onActivityResult() to restart or process the new data
For example, to start Activity B:
String callingActivity = context.getLocalClassName();
Intent newActivity = new Intent(getApplicationContext(),ActivityB.class);
newActivity.setData(Uri.parse(callingActivity));
startActivityForResult(newActivity, 0);
Then somewhere in your Activity A class:
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 0){
// do processing here
}
}
The other answers should suffice, but onResume() can be called in cases where the activity is resumed by other means.
To simply restart Activity A when user presses back button from Activity B, then put the following inside the onActivityResult:
if(requestCode == 0){
finish();
startActivity(starterintent);
}
And in the onCreate of Activity A, add starterintent = getIntent();
Just remember to initiate the variable with Intent starterintent; somewhere before your onCreate is called.
e.g.
public class ActivityA extends ListActivity {
Intent starterintent;
public void onCreate(Bundle b){
starterintent = getIntent();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 0){
finish();
startActivity(starterintent);
}
}
private void startActivityB(){
String callingActivity = context.getLocalClassName();
Intent newActivity = new Intent(getApplicationContext(),ActivityB.class);
newActivity.setData(Uri.parse(callingActivity));
startActivityForResult(newActivity, 0);
}
}
Then just call startActivityB() from a button click or whatever
YES you are right. Write code in onResume.
When you updated date just call notifyDataSetChanged(); for your ListView adapter
Hope, it help you!
You can either start the activity when user press on Save, and it will fix it for you.
Or if you want to press back:
#Override
public void onResume(){
super.onResume();
list.clear();
list.addAll(getBooks());
adapter.notifyDataSetChanged();
}
How can I detect if an activity came to focus after pressing the back button from a child activity, and how can I execute some code at that time?
One possibility would be to start your child activity with startActivityForResult() and implement onActivityResult() which will be called when you return from the child activity.
js's answer is right, but here is some debugged code.
Declare the request code as a constant at the top of your activity:
public static final int OPEN_NEW_ACTIVITY = 12345;
Put this where you start the new activity:
Intent intent = new Intent(this, NewActivity.class);
startActivityForResult(intent, OPEN_NEW_ACTIVITY);
Do something when the activity is finished. Documentation suggests that you use resultCode, but depending on the situation, your result can either be RESULT_OK or RESULT_CANCELED when the button is pressed. So I would leave it out.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == OPEN_NEW_ACTIVITY) {
// Execute your code on back here
// ....
}
}
For some reason, I had trouble when putting this in a Fragment. So you will have to put it in the Activity.
The method you're looking for may be the onResume method you can implements in your mother class ;). You must know that the onResume is also called the first time you launch any activity. Look at the lifecycle of an activity : http://developer.android.com/images/activity_lifecycle.png
Regards,
You can also override both the onBackPressed() method and the onOptionsItemSelected() method and put some logic there. For example I put this into my BaseActivity which all the other Activities extends from:
#Override
public void onBackPressed() {
// your logic
super.onBackPressed();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
// your logic
}
return super.onOptionsItemSelected(item);
}