点击国家/地区名称并保存已打开的活动,每次打开应用,显示上次选择的国家/地区活动,我想添加更改国家/地区

时间:2017-07-26 03:42:37

标签: java android sharedpreferences

这个应用程序中有很多国家,包括印度,巴基斯坦,阿联酋,南非......等等。单击国家/地区名称时,将打开一个新活动。它将通过共享偏好保存,当我们打开应用程序时,将打开最后一个活动。我创建了这样的应用程序。我想在这个应用程序中添加一个东西。

我想通过打开的活动中的按钮转到国家/地区列表视图。用户可以在此处更改国家/地区,在打开应用程序时,必须打开已更改的国家/地区的活动。如果所有国家/地区都像这样开放,则必须有选择其他国家的选项。我希望你能为我做这件事。

由我完成的项目代码如下。

共享偏好将适用于它。我想要一个选项来更改国家/地区选项。

**国家Listview(MainActivity)**

    public class MainActivity extends AppCompatActivity {

    CardView ind,pak,uae,south;
    String clickedCard;
    SharedPreferences prefs;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.activity_main);

        getSupportActionBar().hide();

        ind = (CardView) findViewById(R.id.ind);
        pak = (CardView) findViewById(R.id.pak);

        ind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent CltButton = new Intent(MainActivity.this, India.class);
                clickedCard = "Button 1";
                CltButton.putExtra("fromMain", clickedCard);
                startActivity(CltButton);

            }

        });    

        pak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mgButton = new Intent(MainActivity.this,Pak .class);
                clickedCard = "Button 2";
                mgButton.putExtra("fromMain2", clickedCard);
                startActivity(mgButton);    

            }

        });               

    }

    private void checkPreferences() {

        ////INDIA Preference    
        prefs = getSharedPreferences("pref", MODE_PRIVATE);
        if (prefs.getString("txt", "").equals("") || prefs.getString("lastActivity", "").equals("")) {

        } else {    
            String txt = prefs.getString("txt", "");
            String activity = prefs.getString("lastActivity", "");
            Intent CltButton = new Intent(MainActivity.this, India.class);
            CltButton.putExtra("fromMain", txt);
            startActivity(CltButton);
            finish();
        }


        ////PAKISTAN Preference       
        prefs = getSharedPreferences("pref2", MODE_PRIVATE);
        if (prefs.getString("txt2", "").equals("") || prefs.getString("lastActivity2", "").equals("")) {       

        } else {    
            String txt2 = prefs.getString("txt2", "");
            String activity = prefs.getString("lastActivity", "");
            Intent mgButton =MainActivity new Intent(MainActivity.this, Pak.class);
            mgButton.putExtra("fromMain2", txt2);
            startActivity(mgButton);
            finish();
        }

    }

}

印度活动代码

   public class India extends AppCompatActivity {    
    String s;
    SharedPreferences prefs;    
    Button buttonind;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_india);

        buttonind = (Button) findViewById(R.id.buttonind);

        buttonind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mgButton = new Intent(India.this, Main2Activity.class);
                startActivity(mgButton);

            }
        });

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();

        if (bundle == null) {
            s = "no data received";
        } else {
            s = bundle.getString("fromMain");
        }

    }

    @Override
    protected void onPause() {
        super.onPause();    
        prefs = getSharedPreferences("pref", MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("txt", s);
        editor.putString("lastActivity", getClass().getName());
        editor.apply();    
    }
}

Pak活动代码

public class Pak extends AppCompatActivity {    
    String s;
    SharedPreferences prefs;   
    Button buttonpak;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pak);

        buttonpak = (Button) findViewById(R.id.buttonpak);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();

        if (bundle == null) {
            s = "no data received";
        } else {
            s = bundle.getString("fromMain2");
        }

        buttonpak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mgButton1 = new Intent(Pak.this, Main2Activity.class);
                startActivity(mgButton1);

            }
        });

    }

    @Override
    protected void onPause() {
        super.onPause();

        prefs = getSharedPreferences("pref2", MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("txt2", s);
        editor.putString("lastActivity2", getClass().getName());
        editor.apply();
    }
}

1 个答案:

答案 0 :(得分:0)

非常欢迎你 - 我花了一些时间做它,它的工作方式与你想要的完全一样。

MainActivity:

public class MainActivity extends AppCompatActivity {

CardView ind,pak,uae,south;
String clickedCard;
SharedPreferences prefs;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getSupportActionBar().hide();

    ind = (CardView) findViewById(R.id.ind);
    pak = (CardView) findViewById(R.id.pak);
    uae = (CardView) findViewById(R.id.uae);
    south = (CardView) findViewById(R.id.south);

    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();

    if (bundle!= null) {
        SharedPreferences preferences,preferences1,preferences2,preferences3,preferences4;
        SharedPreferences.Editor editor,editor1,editor2,editor3,editor4;
        preferences = getSharedPreferences("pref", Context.MODE_PRIVATE);
        editor = preferences.edit();
        editor.clear();
        editor.apply();
        preferences1 = getSharedPreferences("pref1", Context.MODE_PRIVATE);
        editor1 = preferences1.edit();
        editor1.clear();
        editor1.apply();
        preferences2 = getSharedPreferences("pref2", Context.MODE_PRIVATE);
        editor2 = preferences2.edit();
        editor2.clear();
        editor2.apply();
        preferences3 = getSharedPreferences("pref3", Context.MODE_PRIVATE);
        editor3 = preferences3.edit();
        editor3.clear();
        editor3.apply();
        preferences4 = getSharedPreferences("pref4", Context.MODE_PRIVATE);
        editor4 = preferences4.edit();
        editor4.clear();
        editor4.apply();

    } else {
        checkPreferences();
    }

    //checkPreferences();

    ind.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent CltButton = new Intent(MainActivity.this, India.class);
            clickedCard = "Button 1";
            CltButton.putExtra("fromMain", clickedCard);
            startActivity(CltButton);
            finish();


        }

    });


    pak.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent mgButton = new Intent(MainActivity.this,Pak.class);
            clickedCard = "Button 2";
            mgButton.putExtra("fromMain2", clickedCard);
            startActivity(mgButton);
            finish();


        }

    });

    uae.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent keraButton = new Intent(MainActivity.this, Uae.class);
            clickedCard = "Button 3";
            keraButton.putExtra("fromMain3", clickedCard);
            startActivity(keraButton);
            finish();

        }

    });

    south.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent ButtonactivityIntent = new Intent(MainActivity.this, South.class);
            clickedCard = "Button 4";
            ButtonactivityIntent.putExtra("fromMain4", clickedCard);
            startActivity(ButtonactivityIntent);
            finish();

        }

    });

}

private void checkPreferences() {


    ////INDIA Preference
    prefs = getSharedPreferences("pref", MODE_PRIVATE);
    if (prefs.getString("txt", "").equals("") || prefs.getString("lastActivity", "").equals("")) {


    } else {

        String txt = prefs.getString("txt", "");
        String activity = prefs.getString("lastActivity", "");
        Intent CltButton = new Intent(MainActivity.this, India.class);
        CltButton.putExtra("fromMain", txt);
        startActivity(CltButton);
        finish();
    }

    ////PAKISTAN Preference
    prefs = getSharedPreferences("pref2", MODE_PRIVATE);
    if (prefs.getString("txt2", "").equals("") || prefs.getString("lastActivity2", "").equals("")) {


    } else {

        String txt2 = prefs.getString("txt2", "");
        String activity = prefs.getString("lastActivity", "");
        Intent mgButton = new Intent(MainActivity.this, Pak.class);
        mgButton.putExtra("fromMain2", txt2);
        startActivity(mgButton);
        finish();
    }



    ////U A E Preference
    prefs = getSharedPreferences("pref3", MODE_PRIVATE);
    if (prefs.getString("txt3", "").equals("") || prefs.getString("lastActivity3", "").equals("")) {


    } else {

        String txt2 = prefs.getString("txt3", "");
        String activity = prefs.getString("lastActivity", "");
        Intent mgButton = new Intent(MainActivity.this, Uae.class);
        mgButton.putExtra("fromMain3", txt2);
        startActivity(mgButton);
        finish();
    }


    //// SOUTH AFRICA Preference
    prefs = getSharedPreferences("pref4", MODE_PRIVATE);
    if (prefs.getString("txt4", "").equals("") || prefs.getString("lastActivity4", "").equals("")) {


    } else {

        String txt2 = prefs.getString("txt4", "");
        String activity = prefs.getString("lastActivity", "");
        Intent mgButton = new Intent(MainActivity.this, South.class);
        mgButton.putExtra("fromMain4", txt2);
        startActivity(mgButton);
        finish();
    }
}

}

India.java:

public class India extends AppCompatActivity {

String s;
SharedPreferences prefs;

Button buttonind;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_india);

    buttonind = (Button) findViewById(R.id.buttonind);

    buttonind.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String test = "";
            Intent mgButton2 = new Intent(India.this, MainActivity.class);
            mgButton2.putExtra("test", test);
            startActivity(mgButton2);
            finish();

        }
    });

    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();

    if (bundle == null) {
        s = "no data received";
    } else {
        s = bundle.getString("fromMain");
    }
}

@Override
protected void onPause() {
    super.onPause();

    prefs = getSharedPreferences("pref", MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString("txt", s);
    editor.putString("lastActivity", getClass().getName());
    editor.apply();
}
}

Pakistan.java

public class Pak extends AppCompatActivity {

String s;
SharedPreferences prefs;

Button buttonpak;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pak);


    buttonpak = (Button) findViewById(R.id.buttonpak);


    buttonpak.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String test = "";
            Intent mgButton2 = new Intent(Pak.this, MainActivity.class);
            mgButton2.putExtra("test", test);
            startActivity(mgButton2);
            finish();


        }
    });

    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();

    if (bundle == null) {
        s = "no data received";
    } else {
        s = bundle.getString("fromMain2");
    }
}

@Override
protected void onPause() {
    super.onPause();

    prefs = getSharedPreferences("pref2", MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString("txt2", s);
    editor.putString("lastActivity2", getClass().getName());
    editor.apply();
}
}

您可以为所有其他国家/地区重复此过程。在MainActivity我可以简化if (bundle!= null),但它正在运行。