Domanda

I applied view pager swipe where it switch over to image and after that i kept a button,where it switch over to another activity.When i open next time, it should not show swipe image.It should directly open Activity.How to do this? I used preference but i don't know where to implement.

My code are as follows:

MainActivity.java

public class MainActivity extends FragmentActivity {
static final int ITEMS = 2; //here i am displaying two times of ImageFragment
MyAdapter mAdapter;
ViewPager mPager;
String status, city;
boolean proceed = true;

public static final String City = "city";
public static final String Activity = "activity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mAdapter = new MyAdapter(getSupportFragmentManager());
    mPager = (ViewPager) findViewById(R.id.pager);
    status = preference.readString(getBaseContext(), preference.LOGGED_IN,
    "");
   city = preference.readString(this, preference.CITY, "");
   if (city.equals("") || (city == null)) {
proceed = false;
    }
    if ((status == "") || (status == null)) {
status = "true";
preference.writeString(getBaseContext(), preference.LOGGED_IN,
        status);
    } else if ((status.equals("true") && (proceed))) {
Intent i = new Intent(MainActivity.this, MainActivity1.class);
i.putExtra("activity", "city");
startActivity(i);
finish();
    }


    mPager.setAdapter(mAdapter);

    Button button = (Button) findViewById(R.id.first);
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            mPager.setCurrentItem(0);
        }
    });
    button = (Button) findViewById(R.id.last);
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            mPager.setCurrentItem(ITEMS);
        }
    });
   }

public static class MyAdapter extends FragmentPagerAdapter {
    public MyAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    @Override
    public int getCount() {
        return ITEMS;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0: // Fragment # 0 - This will show image
            return ImageFragment.init(position);
        case 1: // Fragment # 1 - This will show image
            return ImageFragment.init(position);
        default:// Fragment # 2-9 - Will show list
            return ImageFragment.init(position);
        }
    }
} 

ImageFragment.java

      public class ImageFragment extends Fragment {
        int fragVal;
        Button imagev;
        static ImageFragment init(int val) {
        ImageFragment truitonFrag = new ImageFragment();
        // Supply val input as an argument.
        Bundle args = new Bundle();
        args.putInt("val", val);
        truitonFrag.setArguments(args);
        return truitonFrag;
    }

       @Override
       public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fragVal = getArguments() != null ? getArguments().getInt("val") : 1;
        }

       @Override
       public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View layoutView = inflater.inflate(R.layout.fragment_image, container,
                false);
        View tv = layoutView.findViewById(R.id.text);
        imagev=(Button)layoutView.findViewById(R.id.imageView2);
        imagev.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // here i am switching to another activity. when i open second time, this  
         // MainActivity1 to get displayed
        Intent myIntent = new Intent(getActivity(), MainActivity1.class); 
       getActivity().startActivity(myIntent); 
    }
    });
        ((TextView) tv).setText("Truiton Fragment #" + fragVal);
        return layoutView;
    }
}

MainActivity1.java

    public class MainActivity1 extends Activity{

    public static final String CITY = "city";
    public static final String ACTIVITY = "activity";
    String city,activity;
     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main1);

            city = preference.readString(this, preference.CITY, "");
            Intent i = getIntent();
            activity = i.getStringExtra(ACTIVITY);
     }
}

But if i open second time, it doesn't display MainActivity1.java.Once gain it shows MainActivity.How to solve this? Please help me. And my preference code is here.

http://pastie.org/8405754

Thanks in advance.

È stato utile?

Soluzione 2

Actually I don't know what your fancy string comparisons are used for... Just do it like the following code:

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    if(sp.getBoolean("first_start", true)){
        //Setup everything for the first start, e.g. your ViewPager, Adapter etc.

        //Set first_start = false
        sp.edit().putBoolean("first_start", false).commit();
    }
    else{
        //It's not the first start so go to the other Activity
        Intent i = new Intent(MainActivity.this, MainActivity1.class);
        i.putExtra("activity", "city");
        startActivity(i);
        finish();
    }

Implement this code directly in your onCreate() method.

Altri suggerimenti

/*
         * Metoda ktora zwraca true jezeli apka zostala pierwszy raz uruchomiona po zainstalowaniu
         */
        private boolean isFirstTime() {
            SharedPreferences preferences = getPreferences(MODE_PRIVATE);
            boolean ranBefore = preferences.getBoolean("RanBefore", false);
            if (!ranBefore) {
                SharedPreferences.Editor editor = preferences.edit();
                editor.putBoolean("RanBefore", true);
                editor.commit();
            }
            return !ranBefore;
        }

Return true if first time run application, in onCreate set:

    if (isFirstTime()) {
          do something....
        }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top