sharepreference

xiaoxiao2021-02-28  81

fragment01

package com.bwie.demo1.Fragment;

import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import com.bwie.demo1.R; import com.bwie.demo1.SecondActivity; /**  * Created by k'k'k on 2017/8/6.  */ public class Fragment3 extends Fragment {     private View view;     @Nullable     @Override     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {         view = inflater.inflate(R.layout.frag3, container, false);         return view;     }     @Override     public void onActivityCreated(@Nullable Bundle savedInstanceState) {         super.onActivityCreated(savedInstanceState);         TextView f3 = (TextView) view.findViewById(R.id.f3);         Button but = (Button) view.findViewById(R.id.f3_but);         but.setOnClickListener(new View.OnClickListener() {                                    @Override                                    public void onClick(View v) {                                        SharedPreferences start = getActivity().getSharedPreferences("Start", Context.MODE_PRIVATE);                                        Intent intent = new Intent(getActivity(), SecondActivity.class);                                        startActivity(intent);                                        SharedPreferences.Editor edit = start.edit();                                        edit.putBoolean("pd", true);                                        edit.commit();                                    }                                }         );     }

}

MainActivity

package com.bwie.demo1; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import com.bwie.demo1.Fragment.Fragment1; import com.bwie.demo1.Fragment.Fragment2; import com.bwie.demo1.Fragment.Fragment3; import java.util.ArrayList; public class MainActivity extends AppCompatActivity {     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         ViewPager vp = (ViewPager) findViewById(R.id.vp);         final ArrayList<Fragment> list = new ArrayList<Fragment>();         Fragment1 fragment1 = new Fragment1();         Fragment2 fragment2 = new Fragment2();         Fragment3 fragment3 = new Fragment3();         list.add(fragment1);         list.add(fragment2);         list.add(fragment3);         vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {             @Override             public Fragment getItem(int position) {                 return list.get(position);             }             @Override             public int getCount() {                 return list.size();             }         });         SharedPreferences start = getSharedPreferences("Start", MODE_PRIVATE);         boolean pd = start.getBoolean("pd", false);         if (pd == true) {             Intent intent = new Intent(MainActivity.this, SecondActivity.class);             startActivity(intent);         }     } }

转载请注明原文地址: https://www.6miu.com/read-68564.html

最新回复(0)