简体   繁体   English

将rand变量从一类传递到具有相同值的另一类

[英]Passing rand variable from one class to another with same value

Trying to pass a random generated variable in one class to be used in another to sync up the background of my application and the header in another fragment. 试图在一个类中传递一个随机生成的变量,以供另一个类使用,以同步我的应用程序的背景和另一个片段中的标头。

public class LoginMain extends AppCompatActivity {
    RelativeLayout loginMain;
    Random rand = new Random();
    int bgPick = rand.nextInt(5) + 1; //distribute int from 1 to 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_main);
        //must be called after the content view is set.
        loginMain = (RelativeLayout) findViewById(R.id.activity_login_main);
        randomBG();//method that determines the background image based on the bgPick value.
    }

    public static int getBgPick(){
        return bgPick;
    }

Main Class: 主类:

public class MainActivity extends AppCompatActivity {
    AppBarLayout appBarLayout;
    int bgPick = LoginMain.getBgPick();


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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
        randomHeader();//method to determine header image based on bgPick value determined in the LoginMain class.

This results in two bgPick values being made and used separately in each class. 这将导致在每个类中分别创建和使用两个bgPick值。

You should consider using intent For example, if you want to pass items from one class to another. 您应该考虑使用Intent例如,如果您要将项目从一个类传递到另一个类。

 Intent intent =new Intent(FirstActivity.this,SecondAcitivity.class);
 intent.putExtra("EXTRA_SESSION_ID", sessionId);
 intent.putExtra("EXTRA_String", "California");
 startActivity(intent);

In the SecondActivity; 在SecondActivity中;

String s = getIntent().getStringExtra("EXTRA_SESSION_ID");
String place = getIntent().getStringExtra("EXTRA_String");

Solved by using Static and getBgPick Method. 通过使用StaticgetBgPick方法解决。

public class LoginMain extends AppCompatActivity {
    RelativeLayout loginMain;
    Random rand = new Random();
    public static int bgPick;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_main);
        this.bgPick = rand.nextInt(5) + 1; //distribute int from 1 to 3
        //must be called after the content view is set.
        loginMain = (RelativeLayout) findViewById(R.id.activity_login_main);
        randomBG();
    }

    public static int getBgPick(){
        return bgPick;
    }
}

Main Class: 主类:

public class MainActivity extends AppCompatActivity {    
    AppBarLayout appBarLayout;
    int bgPick = LoginMain.getBgPick();


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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
        randomHeader();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM