Reputation: 297
i have a alert dialog where if the user have no alarms send the user to the alarms section, the dialog and alarm section are in the same activity but diferent fragments
my dialog is from "LifeFragment.java" and the "alarmsfragment on "alarmsfragment.java"
how can i send the user to the other fragment?
Update
With this i tell the activity where to begin:
tabs.setViewPager(pager);
pager.setCurrentItem(2);
I have to do something like this?:
Intent myIntent = new Intent(v.getContext(),
HomeScreenActivity.class);
myIntent.putExtra("1", ViewPager);
Upvotes: 1
Views: 868
Reputation: 7474
1. fire activity from dialog (if its not allready fired else add flag):
Intent intent = new Intent(context, Activity.class);
define key for extra in activity
put extra which fragment u wanna show
intent.putExtra(Activity.STRING_KEY, Activity.VALUE);
context.startActivity(intent);
2. when activity loads:
public static String STRING_KEY = "myKey"; public static String VALUE1 = "fragment1"; public static String VALUE2 = "fragment2"; private String _extra; private int _fragmentNumber;
if(savedInstanceState != null) { _extra = getIntent().getStringExtra(STRING_KEY); // or IntegerExtra(key) }
ViewPager pager = (ViewPager) findViewById(R.id.pager); pager.setAdapter(Adapter);
3. change fragment via viewpager in hosted activity:
( for example use if or switch statement on received "extra")
// if example:
if(_extra != null && _extra == VALUE) {
// change here fragment
pager.setCurrentItem(numberOfFragmentToShow)
}
// switch example:
switch(_extra){
case VALUE1:
_fragmentNumber = 1;
break;
case VALUE2
_fragmentNumber = 2
break;
default:
_fragmentNumber = 3;
}
pager.setCurrentItem(_fragmentNumber);
instead of String extra value you can use Integer value and use it directly on pager
here u got detailed example :
Selecting a specified page in ViewPager when starting
Upvotes: 3
Reputation: 749
You have to redirect to your activity, sending some data, for example in a Bundle, and based on this data you decide which fragment your activity should show (and if the activity should call some method of the fragment.
The intents are used to start only activities or services, not fragments.
Upvotes: 1