【问题标题】:One Widget with Multiple Configuration Activities (Android)一个具有多个配置活动的小部件 (Android)
【发布时间】:2011-10-11 13:40:12
【问题描述】:

我想创建一个小部件应用程序,添加后将启动 ConfigurationActivity,其中有一些选项可供选择。

然后,您可以单击Next 按钮转到NextActivity 并配置您在第一个屏幕上选择的各个选项。

然后,单击Finish 按钮并立即返回带有小部件的主屏幕。

这可以通过我在 XML 中定义的配置活动来完成,还是我必须在 onEnabled() 中执行 startActivity,然后以这种方式更新我的小部件?

感谢您的帮助。

【问题讨论】:

    标签: android configuration android-activity widget


    【解决方案1】:

    您完全可以通过您在 xml 中定义的配置活动来执行此操作。只需让您的第一个活动开始一个指向您的第二个活动的意图,但使用startActivityForResult() 方法。然后在您的第二个活动中,当用户单击第二个活动中的“完成”按钮时,第二个活动将调用finish() 方法。但在调用完成之前,请将结果设置为您在第二个活动中收集的所有数据。然后控制将返回到第一个活动,您可以在其中处理您从 onActivityResult() 方法中的第二个活动获得的结果。然后只需将第二个活动的结果添加到您要从此活动返回的结果中。

    好的,让我们看一个简单的例子。

    ConfigActivity1 extends Activity{
    
      protected onCreate(Bundle icicle){
        //do your setup stuff here.
    
        //This is the button that's going to take us to the next Config activity.
        final Button nextConfig = (Button)findViewById(R.id.next_config);
        //We'll add an onClickListener to take us to the second config activity
        nextConfig.setOnClickListener(new View.OnClickListener(){
          public void onClick(View view){
            //Construct Intent to launch second activity
            Intent furtherConfigIntent = new Intent(ConfigActivity1.this, ConfigActivity2.class);
            //By using startActivityForResult, when the second activity finishes it will return to
            //this activity with the results of that activity.
            startActivityForResult(furtherConfigIntent, 0);
          }
        });
        //finish any other setup in onCreate
      }
    
      //This is a callback that will get called when returning from any activity we started with the
      //startActivityForResult method
      protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if(resultCode == Activity.RESULT_CANCELED){
          //this means the second activity wasn't successfull we should probably just 
          //return from this method and let the user keep configuring.
          return;
        }
        //ok, if we made it here, then everything went well in the second activity.
        //Now extract the data from the data Intent, compile it with the results from this
        //acitivity, and return them. Let's say you put them in an Intent called resultsIntent.
        setResult(Activity.RESULTS_OK, resultsIntent);
        finish();
      }
    
    
    }
    

    第二个活动将非常简单。只需收集您的配置数据,当用户按下完成时,将结果数据和 resultCode 设置为 OK,然后完成。

    【讨论】:

    • 谢谢。看起来这回答了我的问题。 :)
    • 问题是关于一个小部件。但答案是关于一个应用程序(这要容易得多)。所以我看不出这是如何回答这个问题的。
    • @BobUeland 这个问题是相当开放的,但它确实说“小部件应用程序”,我觉得这给了我在一个相当广泛的问题中回答它的许可。提问者似乎也认为我回答了这个问题。如果您想要更具体的内容,最好提出一个新问题。
    猜你喜欢
    • 1970-01-01
    • 2013-07-02
    • 2016-01-15
    • 2014-06-11
    • 1970-01-01
    • 2015-12-31
    • 2012-04-21
    • 1970-01-01
    • 2020-07-16
    相关资源
    最近更新 更多