How we can change selected activity

て烟熏妆下的殇ゞ 提交于 2019-12-13 22:50:11

问题


The app is a University APP, My application contains two buttons in mainactivity(calicutuniversity and mguniversity ), if we click on a button then it goes to the corresponding activity and saves that activity as the default for further application usage. Now i need to try some more ideas into my application. I think you guys can help me for that. I need to.....👇🏻 * when we selects the university from the mainactivity and it goes to the corresponding activity. So if we need to change the activity we also have an option to change the selected university. By clicking change university ,it goes to main activity and then we can select another university and sets it as the default .

Will you please help me ?? My Code Below

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 Activity

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 Activity

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

}


回答1:


Add an Activity before your MainActivty.java that will check the SharedPreferences to load to the activity you want. I added an activity called ReallyEmptyActivity

here's the 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();

    }
}

This will act as your director to the specific activity as upon the user. Your mainactivity.java will be the main screen where the user selects the university.

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

when the user clicks the back button it'll take him back to the MainActivty where they can select the University again and when the app is re-run, the corresponding activity will be shown as the SharedPreference is getting overwritten.




回答2:


When the user is opening the app for the first time check the value of the Shared Preferences and it should take the default value if it does then open the MainActivity.

Else go to that particular activity.

And whenever the user chooses something from the MainActivity then just edit the contents of the Shared Preferences.



来源:https://stackoverflow.com/questions/45901711/how-we-can-change-selected-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!