sharepreference

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);
        }


    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值