Suppose I have 2 classes that extends Activity; ActivityA and ActivityB, and they are declared properly in the manifest XML.
Now, what I want to do is call ActivityB from ActivityA and access ActivityB’s fields.
Intent myIntent = new Intent(MyApplication.getInstance().getApplicationContext(), ActivityB.getClass());
startActivityForResult(myIntent,0);
This code does not return an object of ActivityB, I have tried instantiation manually such that:
ActivityB actB=new ActivityB();
That’s ok, but how to pass that to an intent?
Is that possible?
Please don’t.
First, short of passing activities around via static data members or via a service, it is impossible.
Second, you will create garbage collection issues, as Android will want to destroy old activities to free up memory, but after it does, you will still be holding references to those activities from other activities, negating the work.
Third, you will sometimes be holding destroyed instances of the other activities, for the same reason as in the previous point — Android will go on destroying activities whether you like it or not.
I am fairly confident that whatever problem you think you are solving this way can be solved in some other way.
EDIT
Normally, you pass data from ActivityA to ActivityB via
Intentextras on theIntentpassed tostartActivity(). It is ActivityB’s responsibility to extract those extras and do something with them.