How to get Activity from which second Activity was started? - android

I have activity A, MyRecyclerAdapter, and activity B.
In activity A I build MyRecyclerAdapter and start new activity B from recycler item click.
Now I need to access activity A in onDestroy method from activity B.
How can I do it?
Update:
I tried:
ActivityA parent = (ActivityA) getParent();
parent.setRead(id);
But it gives me null. I think it's because A is not a direct child of B;

For this purpose you should use startActivityForResult(intent). Then you override onActivityResult() in activity A to handle the data you receive after activity B was destroyed. In onDestroy() you'd just have to set the result with setResult(resultCode, data).
Like this you don't need to know about activity A in activity B.

I think you should create an interface class declare a method and implement it in Activity A. Then call that method from Activity B.
Alternatively you can create a Runnable in Activity A which does what you want in the run() method. Then pass the instance of that Runnable with the intent data to Activity B. Then call run on that instance object from Activity B. That should execute the code in the run() method in Activity A.

Related

Resume Parent Activity

I have 2 Activity A and B, A calls B so, I want to Resume parent Activity (A) when B calls finish() on its Activity. Any advice will be useful.
UPDATE:
Maybe I should mention that I use fragments, each Activity has its own fragment, I call finish() from fragment hosted inside B activity and I expect to receive Resume on fragment belongs to A.
When you call Activity B, Activity A will go to background. Unless you finish Activity A while starting Activity B, it will automatically resume when Activity B finishes.
If you are calling this,
startActivity(ActivityB.class, this);
finish();
Just remove the finish(). It should work as expected.

getIntent().getStringExtra() eventually returning null

So in Activity A I pass some data to activity B through an intent. Ok, everything is fine and getStringExtra returns what I expect. Then from activity B, I pass the same data to activity C. Then, when I hit the back button in the toolbar (because of getSupportActionBar().setDisplayHomeAsUpEnabled(true)), the getStringExtra in activity B is now null.
So the flow is A (passes a string)-> B(passes the same string) -> C (back button in toolbar) -> B and now the variable passes from A to B is null. How can I fix that?
Mark this a rule : Whenever you are using the toolbar back button, you should take care of specifying the launchMode of the parent activity.
In your case, what happens after you press back button in Activity C, depends on what launchMode have you specified for your activity B.
If you have't specified any launch mode, the default launch mode is standard. In this case, the parent activity (B) is popped off the stack, and a new instance of that activity is created on top of the stack to receive the intent.
If you have specified launch mode as singleTop, the parent activity is brought to the top of the stack, and receives the intent through its onNewIntent() method. That is, the previous activity is preserved.
Refer http://developer.android.com/training/implementing-navigation/ancestral.html#NavigateUp.
In your case, you want to preserve the variable, therefore you should use singleTop launchMode.
If you are extracting the data in onCreate method of Activity B then when you hit the back button in Activity C, the onCreate method of Activity B is not called again. To get more clarity on the life cycle of an Activity I really suggest you go through this developer.android.com/reference/android/app/Activity.html
Coming to your question when you start Activity B from Activity A, store the string that you sent from A to B in a global variable rather than a local one, and perform check like
public Class B extends Activity {
private String stringData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xml_layout);
if(getIntent().hasExtra(yourKeyName)) {
stringData = getIntent().getStringExtra(yourKeyName);
}
//Do other stuff
}
}
In your activity B, always check before getting the String -
if(getIntent().hasExtra(KEY_NAME))
String s = getIntent().getStringExtra(KEY_NAME);
Reason being that when you press the back button then Activity B is not launched with the same intent that you create in Activity A. Hence the extras are not present.
You should be string the string extra as a global variable and always use a hasExtra check.
I think it is different processes.
The first one, when you start activity B from activity A, you pass
the data from activity A to activity B. It is obvious, so the data
will be present.
The second one, then you start activity C from activity B, because of
activity B is not on the foreground, then your data will be erased.
You can persist your data on activity B by many ways.
One of them you can implement a static global variable.
Second way, you can save your variables on activity B by saving the
data onSaveInstanceState and retrieve from onRestoreInstanceState or
onCreate
Third way you can listen onActivityResult on activity B from activity
C. When you starting activity C, you must use startActivityOnResult
and passing the data that you want to pass back on activity B. After
you finish your activity C, you must return the data that originally
from activity B.
And so on...
But the better way to achieve that is the second way, use bundle onSaveInstanceState and retrieve the bundle data when onCreate.

start activity from Back-Stack without call method on create in Activity

Activity A Call Activity B And B Call C i want to return to A ,Activity A is in BackStack how to return it without calling method onCreate in Activity
By using this code onCreate method will not call
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
How about call the finish() of Activity B when you call the Activity C.

Call Activity1's method from Activity 2

I have an Activity2 which needs to call method update() inside Activity1. How can I do this?
I don't want to start Activity2, just call it's method (this is because activity1 is the viewpager and activity2 is a fragment, so technically, activity1 is already visible as it is holding activity2.
Call getActivity () inside fragment, cast it to your class and use method.
Activity can't hold another activity. If you mean that from first activity you will start intent with second activity which contains fragment, and you want to call update() method from activity2, you can't do that. You need call update() in onActivityResult().
If "Activity2" is a fragment that has been started from Activity1 then this is what I would do:
((Activity1)getActivity()).someMethod();
new Activity(context).methodname();
or
Activity.methodname();

How to Pass object from sub activity to main activity?

From ActivityA I'm starting ActivityB.
In ActivityB I'm creating a new Serializable object.
After the object has been created I want to close ActivityB and pass the new object to ActivityA.
How can I do it?
start Activity B with startActivityForResult().
In activity B, when the object is created create an Intent to pack the object in:
Intent result = new Intent();
result.putExtra("result", object);
setResult(RESULT_OK, result);
Then you will receive that intent in the onActivityResult() method of Activity A, where you can extract it like so:
data.getSerializableExtra("result");
Start the Activity B using startActivityForResult method.
When you finish creating object call setResult in Activity B. Set Your Data in Intent. You don't need to finish this.
Override function onActivityResult in Activity A. This will be called when you call setResult in Activity B. You can receive the data from Intent passed from Activity B.
But most of the time, you need separate Activities if only you have different screens with different tasks. Otherwise accomplish the task within the same Activity. *(A Good and Standard Practice).*
after the object is created, create an intent object, put that object to that intent and then start activity A. In the Activity A's onRestart() get that intent and from that intent get that object.
Why are you doing that? If you're using activity B only for creating new object, you can do it in a plain simple java class. What are you trying to accomplish?

Categories

Resources