我有3个活动Add_Case1,Add_Case2和Add_Case3。 我正在使用Swipe Gesture在它们之间来回切换。
我的问题是:我正在Add_Case1
我填写了一些数据然后滑动到Add_Case2
活动现在填写Add_Case2
中的数据并滑动到{现在{1}}活动,如果我向后滑动转到Add_Case3
或Add_Case1
,我可以看到我填写的数据仍然存在。但是,当我现在再次从Add_Case2
滑动到Add_Case1
或Add_Case2
时。数据已丢失。
我尝试使用Add_Case3
,android:launchMode="singleTop"
,甚至onSaveInstanceState,但它无效。
这是文件的相关代码
Add_Case1
android:launchMode="singleTask"
Add_Case2
public class Add_case1 extends Activity implements SimpleGestureListener{
private SimpleGestureFilter detector;
public Spinner et5;
public EditText et1,et2,et3,et4;
public static String s1,s2,s3,s4,s5,s6[]; //variables defined as static so that it can be used in
// Add_case3.java directly
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Oncreate()","Add Case 1");
if(savedInstanceState!=null)
{
Log.d("Oncreate() sis","Add Case 1");
et1.setText(savedInstanceState.getString("et1"));
et2.setText(savedInstanceState.getString("et2"));
et3.setText(savedInstanceState.getString("et3"));
et4.setText(savedInstanceState.getString("et4"));
}
setContentView(R.layout.activity_add_case);
et1=(EditText)findViewById(R.id.editText1);
et2=(EditText)findViewById(R.id.editText2);
et3=(EditText)findViewById(R.id.editText3);
et4=(EditText)findViewById(R.id.editText4);
et5=(Spinner)findViewById(R.id.case_stage);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.case_stages, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
et5.setAdapter(adapter);
et5.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int pos, long arg3)
{
int index = arg0.getSelectedItemPosition();
s6=getResources().getStringArray(R.array.case_stages);
s5=s6[index];
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
// Detect touched area
detector = new SimpleGestureFilter(this,this);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// getIntent() should always return the most recent
setIntent(intent);
}
@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(savedInstanceState);
Log.d("OnSaveInstanceState()","Add Case 1");
savedInstanceState.putString("et1", et1.getText().toString());
savedInstanceState.putString("et2", et2.getText().toString());
savedInstanceState.putString("et3", et3.getText().toString());
savedInstanceState.putString("et4", et4.getText().toString());
}
@Override
public boolean dispatchTouchEvent(MotionEvent me){
// Call onTouchEvent of SimpleGestureFilter class
this.detector.onTouchEvent(me);
return super.dispatchTouchEvent(me);
}
@Override
public void onSwipe(int direction) {
//assigning the values entered by user in static data members
s1=et1.getText().toString();
s2=et2.getText().toString();
s3=et3.getText().toString();
s4=et4.getText().toString();
switch (direction) {
case SimpleGestureFilter.SWIPE_LEFT :
Intent openStartingPoint=new Intent("app.schedulawyer.com.Add_case2");
openStartingPoint.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
openStartingPoint.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(openStartingPoint);
//finish();
// break;
}
//Toast.makeText(this, "Left Swipe", Toast.LENGTH_SHORT).show();
}
@Override
public void onDoubleTap() {
// TODO Auto-generated method stub
}
}
Add_Case3
public class Add_case2 extends FragmentActivity implements SimpleGestureListener{
private SimpleGestureFilter detector;
public EditText et6,et7,et8;
DateDialogFragment fragnd,fragpd;
Calendar nownd,nowpd;
TextView date,date_pr;
int a;
public static String s6,s7,s8,s9,s10; //static strings for direct and easy use in Add_case3.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Oncreate()","Add Case 2");
if(savedInstanceState!=null)
{
Log.d("Oncreate() sis","Add Case 2");
et6.setText(savedInstanceState.getString("et6"));
et7.setText(savedInstanceState.getString("et7"));
et8.setText(savedInstanceState.getString("et8"));
}
setContentView(R.layout.activity_add_case2);
et6=(EditText)findViewById(R.id.editText1);
et7=(EditText)findViewById(R.id.editText2);
et8=(EditText)findViewById(R.id.editText3);
nownd = Calendar.getInstance();
nowpd=nownd;
date_pr = (TextView)findViewById(R.id.textView4);
date = (TextView)findViewById(R.id.date_picker);
date_pr.setPaintFlags(date_pr.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
date.setPaintFlags(date.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
date_pr.setText(String.valueOf(nownd.get(Calendar.DAY_OF_MONTH))+"-"+String.valueOf(nownd.get(Calendar.MONTH)+1)+"-"+String.valueOf(nownd.get(Calendar.YEAR)));
date.setText(String.valueOf(nownd.get(Calendar.DAY_OF_MONTH))+"-"+String.valueOf(nowpd.get(Calendar.MONTH)+1)+"-"+String.valueOf(nowpd.get(Calendar.YEAR)));
date_pr.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
a=1;
showDialog();
}
});
date.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
a=2;
showDialog();
}
});
// Detect touched area
detector = new SimpleGestureFilter(this,this);
}
public void showDialog() {
if(a==1)
{
fragnd = DateDialogFragment.newInstance(this, new DateDialogFragmentListener(){
public void updateChangedDate(int year, int month, int day){
date_pr.setText(String.valueOf(day)+"-"+String.valueOf(month+1)+"-"+String.valueOf(year));
nownd.set(year, month, day);
// s6=String.valueOf(day)+"-"+String.valueOf(month+1)+"-"+String.valueOf(year);
Log.d("date picker","Showing up from tv1 next date ="+s6);
}
}, nownd);
fragnd.show(getSupportFragmentManager(), "DateDialogFragment");
}
else if(a==2)
{
fragpd = DateDialogFragment.newInstance(this, new DateDialogFragmentListener(){
public void updateChangedDate(int year, int month, int day){
date.setText(String.valueOf(day)+"-"+String.valueOf(month+1)+"-"+String.valueOf(year));
nowpd.set(year, month, day);
// s7=String.valueOf(day)+"-"+String.valueOf(month)+"-"+String.valueOf(year);
Log.d("date picker","Showing up from textView s7 ="+s7); }
}, nowpd);
fragpd.show(getSupportFragmentManager(), "DateDialogFragment");
}
}
public interface DateDialogFragmentListener{
//this interface is a listener between the Date Dialog fragment and the activity to update the textview's date
public void updateChangedDate(int year, int month, int day);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// getIntent() should always return the most recent
setIntent(intent);
}
@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(savedInstanceState);
Log.d("OnSaveInstanceState()","Add Case 2");
savedInstanceState.putString("et6", et6.getText().toString());
savedInstanceState.putString("et7", et7.getText().toString());
savedInstanceState.putString("et8", et8.getText().toString());
}
@Override
public boolean dispatchTouchEvent(MotionEvent me){
// Call onTouchEvent of SimpleGestureFilter class
this.detector.onTouchEvent(me);
return super.dispatchTouchEvent(me);
}
@Override
public void onSwipe(int direction) {
s6= date_pr.getText().toString();
s7= date.getText().toString();
s8=et8.getText().toString(); //stores remarks
s9=et6.getText().toString(); //stores fee settled
s10=et7.getText().toString(); //stores fee paid
switch (direction) {
case SimpleGestureFilter.SWIPE_RIGHT :
Intent openStartingPoint=new Intent("app.schedulawyer.com.Add_case1");
openStartingPoint.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
openStartingPoint.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(openStartingPoint);
//finish();
break;
case SimpleGestureFilter.SWIPE_LEFT :
Intent openStartingPoint2=new Intent("app.schedulawyer.com.Add_case3");
openStartingPoint2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
openStartingPoint2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(openStartingPoint2);
//finish();
break;
}
}
@Override
public void onDoubleTap() {
}
}
清单
public class Add_case3 extends Activity implements SimpleGestureListener{
private SimpleGestureFilter detector;
Button but;
EditText et9,et10,et11;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_case3);
final DatabaseHandler dbs = new DatabaseHandler(this);
but=(Button)findViewById(R.id.Sumit);
et9=(EditText)findViewById(R.id.editText1);
et10=(EditText)findViewById(R.id.editText2);
et11=(EditText)findViewById(R.id.editText3);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//inserting Add_case1 data in case_info table
Log.d("Insert case: ", "Inserting case..");
dbs.addCase(new Case(Add_case1.s1,Add_case1.s2,Add_case1.s3,Add_case1.s4,Add_case1.s5));
//inserting Add_case2 data in date_info table
Log.d("Insert date: ", "Inserting date..");
dbs.addDate(new Date(Add_case1.s2,Add_case2.s6,Add_case2.s7,Add_case2.s9,Add_case2.s10,Add_case2.s8));
//inserting Add_case3 data in client_info table
Log.d("Insert client: ", "Inserting client..");
dbs.addClient(new Client(Add_case1.s2,et9.getText().toString(),et10.getText().toString(),et11.getText().toString()));
Log.d("Insert: ", "Inserting finish..");
Intent openStartingPoint=new Intent("app.schedulawyer.com.View");
Log.d("view: ", "starting activity view ..");
startActivity(openStartingPoint);
finish();
}
});
// Detect touched area
detector = new SimpleGestureFilter(this,this);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// getIntent() should always return the most recent
setIntent(intent);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
}
@Override
public boolean dispatchTouchEvent(MotionEvent me){
// Call onTouchEvent of SimpleGestureFilter class
this.detector.onTouchEvent(me);
return super.dispatchTouchEvent(me);
}
@Override
public void onSwipe(int direction) {
switch (direction) {
case SimpleGestureFilter.SWIPE_RIGHT :
Intent openStartingPoint=new Intent("app.schedulawyer.com.Add_case2");
openStartingPoint.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
openStartingPoint.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(openStartingPoint);
//finish();
break;
}
// CharSequence str="";
//Toast.makeText(this, "Right Swipe", Toast.LENGTH_SHORT).show();
}
@Override
public void onDoubleTap() {
// Toast.makeText(this, "Double Tap", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:3)
我认为你应该改变完全这样做的方式。如果您真的想使用Activity,可以将数据保存到磁盘或数据库,或者它是小型SharedPreferences。这样您就可以随时获取数据。
但我认为你应该在Activity
中使用1 ViewPager
,1 Fragments
和3 ViewPager
。
您可以在下面找到一个教程: http://developer.android.com/training/animation/screen-slide.html
您还可以在打开新活动时准备好代码,只需使用“新建空白活动”,然后选择“滑动视图+标题条”作为导航类型。之后,您将拥有我在上面描述的行为,然后您可以删除标题条。
答案 1 :(得分:1)
是的@tasomaniac是对的,使用view pager是更好的主意。 在http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/
找到一些好的教程