Android Reusing activities with different data

Paddy1990

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!

Shekh Shagar

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Reusing pipe data for different commands

From Dev

Reusing pipe data for different commands

From Dev

Android tabhost with different activities

From Dev

Android tabhost with different activities

From Dev

Two different methods for sharing data between Android activities

From Dev

Switching Activities with different layouts in Android

From Dev

Android Espresso - Testing on different Activities

From Dev

Different toolbar colors for different activities android

From Dev

Android passing data between activities

From Dev

Android Studio .code for pass data between two edit texts in different activities

From Dev

Reusing transformations with different data in Pentaho data integration Kettle

From Dev

How to link listview items to different activities in android?

From Dev

same GooglePlus client object in different activities android

From Dev

Android - Get same intent from different activities

From Dev

Android - Get same intent from different activities

From Dev

How to use different Activities with TabHost Widget in Android?

From Dev

Android - how to return to different previous activities

From Dev

GWT Activities and places: Reusing modal dialogs?

From Dev

Reusing Action Bar in all activities of application

From Dev

Android gradle two different launcher activities for two different product flavors

From Dev

Load different website urls in different activities using Android WebView

From Dev

How to share data between fragments and activities in android

From Dev

passing data between 2 activities android

From Dev

passing data between activities in android - error

From Dev

How to reference data across multiple Android activities?

From Dev

Passing Data Between Activities in Android App

From Dev

Passing data between Activities Android | java

From Dev

Unable to get bundle data from two different activities

From Dev

how to display different json data between two activities?

Related Related

  1. 1

    Reusing pipe data for different commands

  2. 2

    Reusing pipe data for different commands

  3. 3

    Android tabhost with different activities

  4. 4

    Android tabhost with different activities

  5. 5

    Two different methods for sharing data between Android activities

  6. 6

    Switching Activities with different layouts in Android

  7. 7

    Android Espresso - Testing on different Activities

  8. 8

    Different toolbar colors for different activities android

  9. 9

    Android passing data between activities

  10. 10

    Android Studio .code for pass data between two edit texts in different activities

  11. 11

    Reusing transformations with different data in Pentaho data integration Kettle

  12. 12

    How to link listview items to different activities in android?

  13. 13

    same GooglePlus client object in different activities android

  14. 14

    Android - Get same intent from different activities

  15. 15

    Android - Get same intent from different activities

  16. 16

    How to use different Activities with TabHost Widget in Android?

  17. 17

    Android - how to return to different previous activities

  18. 18

    GWT Activities and places: Reusing modal dialogs?

  19. 19

    Reusing Action Bar in all activities of application

  20. 20

    Android gradle two different launcher activities for two different product flavors

  21. 21

    Load different website urls in different activities using Android WebView

  22. 22

    How to share data between fragments and activities in android

  23. 23

    passing data between 2 activities android

  24. 24

    passing data between activities in android - error

  25. 25

    How to reference data across multiple Android activities?

  26. 26

    Passing Data Between Activities in Android App

  27. 27

    Passing data between Activities Android | java

  28. 28

    Unable to get bundle data from two different activities

  29. 29

    how to display different json data between two activities?

HotTag

Archive