Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android使用不同的数据重用活动_Java_Android - Fatal编程技术网

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

Java Android使用不同的数据重用活动,java,android,Java,Android,嗨,我正在开发一个android应用程序,有两个活动实际上是相同的,但加载不同的数据。我目前有两个活动,有很多重复代码,我觉得我可以通过只使用一个活动来优化它 活动1: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.right_hearing_test); String

嗨,我正在开发一个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);
}
活动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();
}
我将某些数据加载到web视图和活动1中的按钮中,然后用户进行测试,然后将用户带到活动2。在这里,它所做的只是在web视图和按钮中显示不同的数据

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

通过传入上下文,我在这两个活动中使用了很多其他方法,我使用了一个helper类,但是我只想对我在WebView和按钮中显示的不同内容使用一个活动


谢谢你的意见

1)您可以将大多数公共数据放在一个BaseActivity中,然后让两个活动对其进行扩展。然后根据您的要求在各个活动中加载数据。

只需保留一个标志来决定选择哪个选项。。 贝娄将告诉你如何控制它

您可以控制此标志unisg getStringExtra(),putStringExtra() 例如您将从活动课开始您的活动

FromActivity.java

.......    
Intent i = new Intent(FromActivity.this,YourActivity.class);
i.putExtra("Flag","optionone");
startActivity(i);
.......
        @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);
        }
        ....
}

YourActivity.java

.......    
Intent i = new Intent(FromActivity.this,YourActivity.class);
i.putExtra("Flag","optionone");
startActivity(i);
.......
        @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);
        }
        ....
}

显然只有
setContentView
不同?您需要首先确定何时需要显示一个或另一个。我可以不使用一个活动,并在测试完成后找出答案吗?另一个解决方案将是两次使用动作。您可以为要使用同一活动执行的不同任务设置不同的操作。做一些事情。所以,如果您正在启动activity to do 1操作,则在启动activity时,使用类似“Action1”的字符串在Intent中设置一个action。当您的活动开始时,您可以检查您已设置的操作,并执行操作1。同样,当您开始活动时,请执行操作2,然后在意向中设置操作“Action2”。当您的活动开始时,您可以检查操作并执行操作2。因此,我使用意向进入活动1,当我打开该活动时,我是否传递了一个动作?然后在测试活动(活动1之后)中,一旦完成并使用意图打开活动2,我将通过另一个操作?然后在共享活动中,我执行一个
if
语句来查找该操作字符串?从您正在启动相关活动的活动中,您将为要执行的不同操作设置不同的操作。在所讨论的活动中,您将检查是否有不同的操作,并相应地执行操作。请使用编码约定来命名操作。这正是我想要的,我不会实现它!非常感谢你的赏识。如果这些对您有帮助,请选择此答案作为已接受答案:)