我将视图寻呼机滑动应用于切换到图像的位置,之后我保留了一个按钮,切换到另一个活动。当我下次打开时,它不应显示滑动图像。它应该直接打开Activity.How去做这个?我使用了偏好但我不知道在哪里实施。
我的代码如下:
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);
}
}
但是,如果我第二次打开,它不会显示MainActivity1.java.Once获得它显示MainActivity.How来解决这个问题?请帮我。我的偏好代码在这里。
提前致谢。
答案 0 :(得分:1)
/*
* 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....
}
答案 1 :(得分:0)
实际上我不知道你花哨的字符串比较用于...就像下面的代码一样:
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();
}
直接在onCreate()方法中实现此代码。