1

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.

fadysi92
  • 61
  • 11
  • Possible duplicate of [How do I pass data between Activities in Android application?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – Manoj Perumarath Mar 22 '17 at 04:16

2 Answers2

0

You should consider using intent For example, if you want to pass items from one class to another.

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

In the SecondActivity;

String s = getIntent().getStringExtra("EXTRA_SESSION_ID");
String place = getIntent().getStringExtra("EXTRA_String");
Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77
  • I don't think I can use this just because I am already using Intent within the same activity to transition to another activity, and when attempting to use .putExtra with randomly generated variables there is some strange behaviour. My solution below solved my problem adequately. – fadysi92 Mar 23 '17 at 22:17
-1

Solved by using Static and getBgPick Method.

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();
    }
}
Himanshu Moradiya
  • 4,769
  • 4
  • 25
  • 49
fadysi92
  • 61
  • 11