Unable To Find Code To Replace Detail Fragment Based On List Item Position
Solution 1:
I think I understand better now. What you really want is...to start another activity and immediately launch the expected fragment, this would be clearer for others. For showing the correct fragment in another activity, you should do it in that activity. There is a good Google webpage @ Starting Another Activity. Below are code samples from that webpage, using your posted code.
Code samples:
WCBankActivity.java:
// Define this for Intent, mainly keep the key consistent between 2 activitiespublicfinalStringEXTRA_MESSAGE="Station_key";
FragmentWCLine.java:
if (mTwoPane) {
...
}
else {
Intentintent=newIntent(this, mWC[position].activityClass);
Stringstation= mWC[position].station;
intent.putExtra(WCBankActivity.EXTRA_MESSAGE, station);
startActivity(intent);
}
In the other activity, WCWATActivity.java, if I understand right:
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
// Get the message from the intentIntentintent= getIntent();
// Notice to specify the sender Activity for the messageStringstation= intent.getStringExtra(WCBankActivity.EXTRA_MESSAGE);
...
FragmentLineChooserListnewFragment=newFragmentLineChooserList();
FragmentTransactiontransaction= getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.detail_container, newFragment);
transaction.commit();
}
}
Notes:
- Notice the key string
EXTRA_MESSAGE
from one activity to another. Define this constant final in WCBankActivity class. - In
onCreate
(), get the station as a message, and process it. - This is a technique to pass data from one Activity to another, Intent.putExtra() and getStringExtra().
getActivity().getSupportFragmentManager()
is used instead of FragmentWCLine.getActivity().
Solution 2:
For now, I see that variable mTwoPane
never gets changed. Regarding your question, code FragmentTransaction transaction = FragmentWCLine
and code related to it is fine to me.
Your top code of FragmentTransaction transaction = FragmentMainList
. This is probably not correct, need code:
FragmentTransactiontransaction= FragmentWCLine
Note:
- FragmentWCLine is correct one, not FragmentMainList.
Suggested code, to be cleaner:
FragmentTransactiontransaction=this.getActivity().getSupportFragmentManager().beginTransaction();
Note: When in a Fragment, getActivity() should be valid. This way you don't need to specify which fragment class your code resides.
Post a Comment for "Unable To Find Code To Replace Detail Fragment Based On List Item Position"