Android重复使用不同数据的活动

Pad*_*990 3 java android

嗨,我正在开发一个Android应用程序,并有两个实际上相同的活动,但加载不同的数据.我目前有两个活动,有很多重复的代码,我觉得我可以通过只使用一个活动来优化它.

活动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);
}
Run Code Online (Sandbox Code Playgroud)

活动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();
}
Run Code Online (Sandbox Code Playgroud)

我将某些数据加载到Web视图和活动1中的按钮,然后用户进行测试,然后将用户带到活动2.这里所做的只是在Web视图和按钮中显示不同的数据.

我的问题是,如果我为两个页面重用一个活动,如何将正确的数据加载到每个页面中,甚至可能?

我通过传递上下文,在两个活动中使用了很多其他方法的辅助类,但我想只对Webview和按钮中显示的不同内容使用一个活动!

感谢您的任何意见!

She*_*gar 6

只需保留一个标志即可决定选择哪个选项.贝娄会告诉您如何控制它.

你可以控制这个标志unisg getStringExtra(),putStringExtra()例如.您将从FromActivity类开始您的活动.

FromActivity.java

.......    
Intent i = new Intent(FromActivity.this,YourActivity.class);
i.putExtra("Flag","optionone");
startActivity(i);
.......
Run Code Online (Sandbox Code Playgroud)

要么

..
Intent i = new Intent(FromActivity.this,YourActivity.class);
i.putExtra("Flag","optiontwo");
startActivity(i);
...
Run Code Online (Sandbox Code Playgroud)

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);
        }
        ....
}
Run Code Online (Sandbox Code Playgroud)