开发者

One Widget with Multiple Configuration Activities (Android)

i want to create a widget app that when added will start ConfigurationActivity which has some options to choose.

Then, you can click on the Next button to go to the NextActivity and configure each individual option you chose on the first screen.

Then, click Finish button and return to the home screen with the widget on it now.

Can this be done from the configuration activity that I 开发者_高级运维define in my XML, or do I have to maybe do a startActivity in the onEnabled() and have then update my widget that way?

Thank you for any help.


You can totally do this from the configuration activity that you define in your xml. Just make your first activity start an intent that goes to your second activity, but use the startActivityForResult() method. Then in your second activity, make it so when the user clicks the Finish button in the second activity, that the second activity calls the finish() method. But before you call finish, set the result to all the data you've collected in this second activity. Control will then return to the first activity where you can handle the results you got from the second activity in your onActivityResult() method. Then just add the results from the second activity to the results that you're going to return from this activity.

Alright, so let's look at a barebones example of this.

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


}

The second acitvity will be pretty straightforward. Just collect your config data, and when the user presses finish, set the result data and the resultCode to OK, and then finish.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜