0

I'm just started to make my first android app and I'm trying to get more familiar with the basic principles of android developing. So, in no time my MainActivity exploded with lines of code. To make my code more maintainable, i'm trying to put pieces of code in different activities. Also according to the design principles of android: Don't Overload a Single Activity Screen

Now I'm struggling to use different activities with a single XML layout. I found some similar cases here like: this one But i'm also reading here that I should use fragments. I can't see how to work this out properly.

The specific problem I encounter with my code is that the second activity should change the background of the imageview to normal with the setImageResource, but it doesn't.

My code:

package com.test.scores;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends Activity implements View.OnClickListener {

    private ImageButton btn1, btn2;
    int varMinusScore;

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

        btn1 = (ImageButton) findViewById(R.id.btn1);
        btn1.setOnClickListener(this);
        btn2 = (ImageButton) findViewById(R.id.btn2);
        btn2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                varMinusScore = 1;
                startActivity(new Intent(getApplicationContext(), ResetImageResources.class));
                btn1.setImageResource(R.drawable.btn01p);
        }
        switch (v.getId()) {
            case R.id.btn2:
                varMinusScore = 2;
                startActivity(new Intent(getApplicationContext(), ResetImageResources.class));
                btn2.setImageResource(R.drawable.btn02p);
        }
    }
}

And the second activity:

package com.test.scores;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageButton;

public class ResetImageResources extends Activity {

     private ImageButton btn1, btn2;

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

        btn1 = (ImageButton) findViewById(R.id.btn1);
        btn2 = (ImageButton) findViewById(R.id.btn2);
        btn1.setImageResource(R.drawable.btn01);
        btn2.setImageResource(R.drawable.btn02);

        finish();
    }
}
Community
  • 1
  • 1
Jeroen
  • 1
  • 1

2 Answers2

2

Activities are absolutely isolated from each others. The same XML file which you are setting as a content of each activity doesn't mean that it same/shared instance of layout. You should think not in terms of layouts, but in terms of activities.

In your case you just start second Activity, change background of buttons here, then go back and see first Activity. Any changes in second Activity would not be mirrored somewhere else. That's it.

Divers
  • 9,531
  • 7
  • 45
  • 88
0

Try this:

Insert a button to finish in second activity. Use finish() under the button interface button.setOnClickListener(new OnClickListener(){} ); then you'll clearly notice the difference between the backgrounds. Only if you click you can go back to main activity.

Quantum_VC
  • 185
  • 13
  • Now I get it. I also understand the reload of the screen which is actualy starting a new instance of the R.layout.activity_main. I didn't get that before. But what should I do? Is there another way to split my code into pieces (and change the same layout) or should I keep all of the code that affects the layout in the MainActivity? – Jeroen Mar 22 '15 at 08:45
  • It's better to maintain one java file for one layout file. Do that according to your functional requirements. – Quantum_VC Mar 23 '15 at 21:32