我们如何改变选定的活动

时间:2017-08-27 04:31:33

标签: javascript java android android-studio sharedpreferences

该应用程序是一个大学APP,我的应用程序包含两个主要活动按钮(calicutuniversity和mguniversity),如果我们点击一​​个按钮然后它转到相应的活动并将该活动保存为默认的进一步应用程序使用。      现在我需要在我的应用程序中尝试更多的想法。我想你们可以帮助我。我需要.....  *当我们从主要活动中选择大学并进入相应的活动时。因此,如果我们需要更改活动,我们还可以选择更改所选大学。通过单击更改大学,它将转到主要活动,然后我们可以选择另一所大学并将其设置为默认大学。

你能帮我吗? 我的代码

MainActivity

public class MainActivity extends AppCompatActivity {

CardView cd1, cd2;
String clickedCard;
SharedPreferences prefs;

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

    cd1 = (CardView) findViewById(R.id.clt);
    cd2 = (CardView) findViewById(R.id.mg);


    checkPreferences();
    ////////Button1

    cd1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent ButtonactivityIntent = new Intent(MainActivity.this, CltActivity.class);
            clickedCard = "Button 1";
            ButtonactivityIntent.putExtra("fromMain1", clickedCard);
            startActivity(ButtonactivityIntent);
            finish();
        }
    });

    ///////////////Button2

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

    ///////////////Button3


}


private void checkPreferences() {

    prefs = getSharedPreferences("pref1", MODE_PRIVATE);
    if (prefs.getString("txt1", "").equals("") || prefs.getString("lastActivity1", "").equals("")) {

        //do nothing
    } else {

        String txt = prefs.getString("txt1", "");

        String activity = prefs.getString("lastActivity1", "");
        Intent ButtonactivityIntent = new Intent(MainActivity.this, CltActivity.class);
        ButtonactivityIntent.putExtra("fromMain1", txt);
        startActivity(ButtonactivityIntent);
        finish();

    }


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

        //do nothing
    } else {

        String txt = prefs.getString("txt2", "");

        String activity = prefs.getString("lastActivity", "");
        Intent ButtonactivityIntent = new Intent(MainActivity.this, MgACtivity.class);
        ButtonactivityIntent.putExtra("fromMain2", txt);
        startActivity(ButtonactivityIntent);
        finish();

    }
}

}

CalicutUniversity活动

public class CltActivity extends AppCompatActivity {


String s;

SharedPreferences prefs;

Button buttonind;

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

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



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

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


    findViewById(R.id.buttonind).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showMenu();
        }
    });

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


        }
    });



}

private void showMenu() {


}

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

    prefs = getSharedPreferences("pref1", MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString("txt1", s);
    editor.putString("lastActivity1", getClass().getName());
    editor.apply();
}

}

Mguniversity活动

public class MgACtivity extends AppCompatActivity {


String s;

SharedPreferences prefs;

Button buttonind;

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

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



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

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


    findViewById(R.id.buttonind).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showMenu();
        }
    });

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


        }
    });



}

private void showMenu() {


}

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

}

2 个答案:

答案 0 :(得分:0)

在MainActivty.java之前添加一个Activity,它将检查要加载到所需活动的SharedPreferences。我添加了一个名为ReallyEmptyActivity的活动

这里是ReallyEmptyActivity.java

public class ReallyEmptyActivity extends AppCompatActivity {

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

        SharedPreferences preferences = this.getSharedPreferences("UNIV", MODE_PRIVATE);

        String uni = preferences.getString("UNIV", "0");
        Log.d("LOAD", uni);

        if (uni.equals("0")) {
            startActivity(new Intent(ReallyEmptyActivity.this, MainActivity.class));
        } else if (uni.equals("uni1")) {
            startActivity(new Intent(ReallyEmptyActivity.this, Uni1.class));
        } else {
            startActivity(new Intent(ReallyEmptyActivity.this, Uni2.class));
        }

        finish();

    }
}

这将作为您的特定活动的主管,作为用户。 您的mainactivity.java将是用户选择大学的主屏幕。

public class MainActivity extends AppCompatActivity {

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

    public void uni1onclick(View view) {
        SharedPreferences preferences = this.getSharedPreferences("UNIV",MODE_PRIVATE);
        SharedPreferences.Editor  editor = preferences.edit();

        editor.putString("UNIV","uni1");
        editor.commit();
        startActivity(new Intent(MainActivity.this,Uni1.class));
    }

    public void uni2onclick(View view) {

        SharedPreferences preferences = this.getSharedPreferences("UNIV",MODE_PRIVATE);
        SharedPreferences.Editor  editor = preferences.edit();
        editor.putString("UNIV","uni2");
        editor.commit();

        startActivity(new Intent(MainActivity.this,Uni1.class));
    }
}

当用户点击后退按钮时,它会将他带回MainActivty,在那里他们可以再次选择大学,当应用程序重新运行时,相应的活动将显示为SharedPreference被覆盖。

答案 1 :(得分:0)

当用户第一次打开应用程序时,请检查共享首选项的值,如果是,则应采用默认值,然后打开MainActivity。

否则请参加该特定活动。

每当用户从MainActivity中选择一些内容时,只需编辑共享首选项的内容。

相关问题