David David - 8 months ago 79
Android Question

Android FragmentPagerAdapter get context

i have this adapter for fragments:

public class TabsPagerAdapter extends FragmentPagerAdapter {
private Context context;

public TabsPagerAdapter(FragmentManager fm) {

public Fragment getItem(int index) {

switch (index) {
case 0:
// Main fragment activity
return new main();
case 1:
// Sensors fragment activity
return new sensors();
case 2:
// Display fragment activity
return new display();
case 3:
// Settings fragment activity
return new settings();

return null;

public int getCount() {
// get item count - equal to number of tabs
return 4;

i'm triing to make a back button exit with double tap and toast in this way:

private static final long DOUBLE_PRESS_INTERVAL = 2000000000;
private long lastPressTime;

public void onBackPressed() {

long pressTime = System.nanoTime();
if (pressTime - lastPressTime <= DOUBLE_PRESS_INTERVAL) {
// this is a double click event

lastPressTime = pressTime;


What i can't make is to get the context in the toast, therefore i cant show it.
How can i modify the code to get it work?
Thank you for your answers!

okay, this tutorial what i'm using:
and i want to make this exit method on each tab


you have to use getActivity()

like this

         Toast.makeText(getActivity() , ......