Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Reusing activities with different data

Tags:

java

android

Hi I'm developing an android application and have two activities that are practically the same, but load different data. I currently have two Activities with a lot of duplicate code and I feel I can optimise it by using only one activity.

Activity 1:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.right_hearing_test);

    String topHtml = this.getString(R.string.top_content);
    String bottomHtml = this.getString(R.string.bottom_content);

    View infoButton = findViewById(R.id.info_button);
    infoButton.setVisibility(View.VISIBLE);

    TextView titleText = (TextView) findViewById(R.id.title_text);
    titleText.setText(R.string.Hearing_Test);

    mScrollButton = (ScrollView) findViewById(R.id.scroll_view);

    topContent = (WebView) findViewById(R.id.top_content);
    topContent.setBackgroundColor(0);

    bottomContent = (WebView) findViewById(R.id.bottom_content);
    bottomContent.setBackgroundColor(0);

    activityHelper = new ActivityHelper(this);

    topContent.loadUrl("file:///android_asset/html/" + topHtml);
    bottomContent.loadUrl("file:///android_asset/html/" + bottomHtml);

    getScreenSize();
    getMargins();

    setResult(RESULT_OK);
}

Activity 2

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.left_hearing_test);

    View infoButton = findViewById(R.id.info_button);
    infoButton.setVisibility(View.VISIBLE);

    mScrollButton = (ScrollView) findViewById(R.id.scroll_view);

    topContent = (WebView) findViewById(R.id.top_content);
    topContent.setBackgroundColor(0);

    bottomContent = (WebView) findViewById(R.id.bottom_content);
    bottomContent.setBackgroundColor(0);

    String topHtml = this.getString(R.string.switch_file);
    String bottomHtml = this.getString(R.string.bottom_content);

    activityHelper = new ActivityHelper(this);

    topContent.loadUrl("file:///android_asset/html/" + topHtml);
    bottomContent.loadUrl("file:///android_asset/html/" + bottomHtml);

    getScreenSize();
    getMargins();
}

I Load certain data into the web views and the button in activity 1, then the user does a test, which then take the user to activity 2. Here all it does is display different data in the web views and the button.

My question is if I reuse one activity for both pages, how do I load the correct data into each one and is it even possible?

I've used a helper class for a lot of other methods I use on both activities by passing the context in, but I would like to use only one activity for the different content I display in the webviews and the button!

Thanks for any input!

like image 224
Paddy1990 Avatar asked Jul 07 '26 00:07

Paddy1990


1 Answers

Simply keep a flag to decide which option to chose.. Bellow will give you an idea how to control it.

You can control this flag unisg getStringExtra(), putStringExtra() For example. you will start your activity from FromActivity class.

FromActivity.java

.......    
Intent i = new Intent(FromActivity.this,YourActivity.class);
i.putExtra("Flag","optionone");
startActivity(i);
.......

or

..
Intent i = new Intent(FromActivity.this,YourActivity.class);
i.putExtra("Flag","optiontwo");
startActivity(i);
...

YourActivity.java

        @Override
        public void onCreate(Bundle savedInstanceState) {

        ......
        ..
        ..
        String flag = String.valueOf(getIntent().getStringExtra("Flag"));

        if(flag.equalsIgnoreCase("optionone")){

            String topHtml = this.getString(R.string.top_content);
            String bottomHtml = this.getString(R.string.bottom_content);

            TextView titleText = (TextView) findViewById(R.id.title_text);
            titleText.setText(R.string.Hearing_Test);


        }else if(flag.equalsIgnoreCase("optiontwo")){
            String topHtml = this.getString(R.string.top_content);
            String bottomHtml = this.getString(R.string.bottom_content);
        }else{
    }
        .....
        ...
        ...
        if(flag.equalsIgnoreCase("optionone")){
            setResult(RESULT_OK);
        }
        ....
}
like image 159
Shekh Shagar Avatar answered Jul 08 '26 14:07

Shekh Shagar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!