在用户输入期间旋转屏幕时如何防止重置
How to prevent resetting when rotating screen during user input
我的主要activityclass:
public class MainActivity extends AppCompatActivity {
private Button mAddProfileButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAddProfileButton = (Button) findViewById(R.id.idAddNewProfileButton);
mAddProfileButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setTitle("Add New Profile");
alert.setMessage("Name:");
final EditText input = new EditText(MainActivity.this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// user pressed OK
String value = input.getText().toString();
Toast.makeText(MainActivity.this, value, Toast.LENGTH_SHORT).show();
// do something with the value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
});
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
}
}
所以基本上在主屏幕上,您可以按一个按钮向我的应用程序添加新的配置文件。当用户单击按钮时,用户输入对话框显示正常,他们可以输入名称,但如果旋转屏幕,它会刷新 activity 并完全删除提示。
有办法保存吗?我熟悉 OnSaveInstanceState
和 OnRestoreInstanceState
背后的基本思想,但我不知道如何让它记住并重新实例化输入对话框以及用户到目前为止输入的内容。
if you rotate the screen, it refreshes the activity and removes the prompt altogether.
不清楚 "the prompt" 是什么。
如果 "the prompt",你的意思是你的 AlertDialog
,那是因为你没有使用 DialogFragment
。将您的 AlertDialog
包裹在 DialogFragment
中,DialogFragment
将为您处理配置更改。
在 this sample app 中,我有一个 DialogFragment
创建我的 AlertDialog
:
/***
Copyright (c) 2012 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/
package com.commonsware.android.dlgfrag;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class SampleDialogFragment extends DialogFragment implements
DialogInterface.OnClickListener {
private View form=null;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
form=
getActivity().getLayoutInflater()
.inflate(R.layout.dialog, null);
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
return(builder.setTitle(R.string.dlg_title).setView(form)
.setPositiveButton(android.R.string.ok, this)
.setNegativeButton(android.R.string.cancel, null).create());
}
@Override
public void onClick(DialogInterface dialog, int which) {
String template=getActivity().getString(R.string.toast);
EditText name=(EditText)form.findViewById(R.id.title);
EditText value=(EditText)form.findViewById(R.id.value);
String msg=
String.format(template, name.getText().toString(),
value.getText().toString());
Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
}
@Override
public void onDismiss(DialogInterface unused) {
super.onDismiss(unused);
Log.d(getClass().getSimpleName(), "Goodbye!");
}
@Override
public void onCancel(DialogInterface unused) {
super.onCancel(unused);
Toast.makeText(getActivity(), R.string.back, Toast.LENGTH_LONG).show();
}
}
然后 activity 只显示 DialogFragment
,在本例中由绑定到布局中 showMe()
方法的 Button
小部件触发:
/***
Copyright (c) 2012 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/
package com.commonsware.android.dlgfrag;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void showMe(View v) {
new SampleDialogFragment().show(getFragmentManager(), "sample");
}
}
我的主要activityclass:
public class MainActivity extends AppCompatActivity {
private Button mAddProfileButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAddProfileButton = (Button) findViewById(R.id.idAddNewProfileButton);
mAddProfileButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setTitle("Add New Profile");
alert.setMessage("Name:");
final EditText input = new EditText(MainActivity.this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// user pressed OK
String value = input.getText().toString();
Toast.makeText(MainActivity.this, value, Toast.LENGTH_SHORT).show();
// do something with the value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
});
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
}
}
所以基本上在主屏幕上,您可以按一个按钮向我的应用程序添加新的配置文件。当用户单击按钮时,用户输入对话框显示正常,他们可以输入名称,但如果旋转屏幕,它会刷新 activity 并完全删除提示。
有办法保存吗?我熟悉 OnSaveInstanceState
和 OnRestoreInstanceState
背后的基本思想,但我不知道如何让它记住并重新实例化输入对话框以及用户到目前为止输入的内容。
if you rotate the screen, it refreshes the activity and removes the prompt altogether.
不清楚 "the prompt" 是什么。
如果 "the prompt",你的意思是你的 AlertDialog
,那是因为你没有使用 DialogFragment
。将您的 AlertDialog
包裹在 DialogFragment
中,DialogFragment
将为您处理配置更改。
在 this sample app 中,我有一个 DialogFragment
创建我的 AlertDialog
:
/***
Copyright (c) 2012 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/
package com.commonsware.android.dlgfrag;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class SampleDialogFragment extends DialogFragment implements
DialogInterface.OnClickListener {
private View form=null;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
form=
getActivity().getLayoutInflater()
.inflate(R.layout.dialog, null);
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
return(builder.setTitle(R.string.dlg_title).setView(form)
.setPositiveButton(android.R.string.ok, this)
.setNegativeButton(android.R.string.cancel, null).create());
}
@Override
public void onClick(DialogInterface dialog, int which) {
String template=getActivity().getString(R.string.toast);
EditText name=(EditText)form.findViewById(R.id.title);
EditText value=(EditText)form.findViewById(R.id.value);
String msg=
String.format(template, name.getText().toString(),
value.getText().toString());
Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
}
@Override
public void onDismiss(DialogInterface unused) {
super.onDismiss(unused);
Log.d(getClass().getSimpleName(), "Goodbye!");
}
@Override
public void onCancel(DialogInterface unused) {
super.onCancel(unused);
Toast.makeText(getActivity(), R.string.back, Toast.LENGTH_LONG).show();
}
}
然后 activity 只显示 DialogFragment
,在本例中由绑定到布局中 showMe()
方法的 Button
小部件触发:
/***
Copyright (c) 2012 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/
package com.commonsware.android.dlgfrag;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void showMe(View v) {
new SampleDialogFragment().show(getFragmentManager(), "sample");
}
}