[英]How to handle the onResume in web service call
我正在开发基于付款的应用程序。 所以在这种情况下,在第一屏用户必须selectproduct
项目,并移动到下一个屏幕,屏幕2他有选择的payment
方式现金或卡。 在屏幕一中,商店名称正在通过Web服务显示。 我已经在同步任务中处理过。如果他从屏幕2中选择现金模式,则付款将被authorized
并显示respective message
。
但是,如果他要从screen 2,screen 1
返回产品screen(screen 1)
screen 2,screen 1
不会再次调用Web服务。 应该是同样的方式。 但是如果payment
completed
,则会刷新。 我该如何实现? 我是否在Resume method
提到整个操作。
在Commonsware上检查以下示例,希望对您有所帮助。
public class RotationAsync extends Activity {
private ProgressBar bar=null;
RotationAwareTask task=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bar=(ProgressBar)findViewById(R.id.progress);
Button next = (Button) findViewById(R.id.next);
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent (RotationAsync.this, First.class);
startActivity(i);
}
});
task=(RotationAwareTask)getLastNonConfigurationInstance();
if (task==null) {
task=new RotationAwareTask(this);
task.execute();
}
else {
task.attach(this);
updateProgress(task.getProgress());
if (task.getProgress()>=100) {
markAsDone();
}
}
}
@Override
public Object onRetainNonConfigurationInstance() {
task.detach();
return(task);
}
void updateProgress(int progress) {
bar.setProgress(progress);
}
void markAsDone() {
findViewById(R.id.completed).setVisibility(View.VISIBLE);
}
static class RotationAwareTask extends AsyncTask<Void, Void, Void> {
RotationAsync activity=null;
int progress=0;
RotationAwareTask(RotationAsync activity) {
attach(activity);
}
@Override
protected Void doInBackground(Void... unused) {
for (int i=0;i<20;i++) {
SystemClock.sleep(500);
publishProgress();
}
return(null);
}
@Override
protected void onProgressUpdate(Void... unused) {
if (activity==null) {
Log.w("RotationAsync", "onProgressUpdate() skipped -- no activity");
}
else {
progress+=5;
activity.updateProgress(progress);
}
}
@Override
protected void onPostExecute(Void unused) {
if (activity==null) {
Log.w("RotationAsync", "onPostExecute() skipped -- no activity");
}
else {
activity.markAsDone();
}
}
void detach() {
activity=null;
}
void attach(RotationAsync activity) {
this.activity=activity;
}
int getProgress() {
return(progress);
}
}
}
第一.java
public class First extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ProgressBar android:id="@+id/progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView android:id="@+id/completed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Work completed!"
android:visibility="invisible"
/>
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next" />
</LinearLayout>
first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back" />
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.