我想创建一个小部件应用程序,添加后将启动 ConfigurationActivity
,其中有一些选项可供选择。
然后,您可以单击 Next
按钮转到 NextActivity
并配置您在第一个屏幕上选择的每个选项。
然后,单击Finish
按钮并返回到现在显示小部件的主屏幕。
这可以通过我在 XML 中定义的配置 Activity 来完成,还是我必须在 onEnabled()
中执行 startActivity
然后更新我的小部件那样吗?
感谢您的帮助。
最佳答案
您完全可以通过在 xml 中定义的配置 Activity 来完成此操作。只需让您的第一个 Activity 启动一个进入您的第二个 Activity 的 Intent ,但使用 startActivityForResult()方法。然后在您的第二个 Activity 中,当用户在第二个 Activity 中单击“完成”按钮时,第二个 Activity 调用 finish()
方法。但在您调用 finish 之前,将结果设置为您在第二个 Activity 中收集的所有数据。然后控件将返回到第一个 Activity ,您可以在 onActivityResult()
方法中处理从第二个 Activity 获得的结果。然后只需将第二个 Activity 的结果添加到您要从此 Activity 返回的结果中。
好吧,让我们看一个简单的示例。
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();
}
}
第二个 Activity 将非常简单。只需收集您的配置数据,当用户按下完成时,将结果数据和 resultCode 设置为 OK,然后完成。
关于android - 具有多个配置 Activity 的一个小部件 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7726982/