问题
The scenerio of my some part of my app is as below:
The problem I'm stuck at is, I'm in the EnterChildExpFragment
. I want to go to ChildDetailsFragment
(which is in ChildExpensesActivity).
I can't share the whole code here. but I can tell some part of what I've been doing until now for going to ChildDetailsFragment
from EnterChildExpFragment
.
First I tried: ((MainActivityExpenses)getActivity()).setCurrentItem(1, true);
But this shows up an error : cannot cast fragmentactivty to activity
.
Then I tried :
ChildDetailsFragment childDetailsFragment = new ChildDetailsFragment();
FragmentTransaction fragTransaction=getFragmentManager().beginTransaction();
fragTransaction.replace(R.layout.child_details_fragment_layout,childDetailsFragment);
fragTransaction.addToBackStack(null);
fragTransaction.commit();
But this also shows error: No view found for id 0x7f...
Note: Pl don't suggest me to use change the design or to merge EnterChi...
fragments in ChildExpenseActivity
.
I've kind of made nested fragments without using parent-child
concept for fragments.
More info: In ChildExpenseActivity
, I've used Viewpager
and loaded the fragments in getItem
method of FragmentStatePagerAdapter
.
AddChildFragmentActivity
is a FragmentActivity and has tabs in ActionBar
.
TIA!
回答1:
After cracking my head and doing some "jugaad" , finally I got want I wanted.
In EnterChildExpFragment
I wrote this code snippet:
MainActivityExpenses.myBoolean=true;
Intent myIntent = new Intent(viewEnterChildExp.getContext(), MainActivityExpenses.class);
myIntent.putExtra("fromEnterChildExpToMainActivityExpenses", "true");
startActivity(myIntent);
getActivity().finish();
And in ChildExpenseActivity
:
public static Boolean myBoolean=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_expenses);
viewPagerMainActivityExpenses = (ViewPager) findViewById(R.id.viewPagerExpenses);
viewPagerMainActivityExpenses.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
// try {
if (myBoolean) {
// myBoolean = getIntent().getExtras().getBoolean("fromEnterChildExpToMainActivityExpenses");
myBoolean=false;
viewPagerMainActivityExpenses.setCurrentItem(1,true);
}
/*} catch (Exception e) {
Log.e("intent error","is : "+e.toString());
}*/
}
private class MyPagerAdapter extends FragmentStatePagerAdapter {
public MyPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0: {
return ChildFragment.newInstance();
}
case 1: {
return ChildDetailsFragment.newInstance();
}
case 2: {
return SomeFragment.newInstance();
}
default: {
return SomeDefaultFragment.newInstance();
}
}
}
@Override
public int getCount() {
return 3;
}
}
public void setCurrentItem(int item, boolean smoothScroll) {
viewPagerMainActivityExpenses.setCurrentItem(item, smoothScroll);
}
来源:https://stackoverflow.com/questions/29002898/go-from-second-fragment-inside-fragment-activity-to-second-fragment-inside-anoth