2014-03-04 71 views
1

我有3個活動Add_Case1,Add_Case2和Add_Case3。 我正在使用Swipe Gesture在它們之間來回切換。在滑動活動和維護數據之間切換

我的問題是:我在Add_Case1我填寫了一些數據,然後滑動到Add_Case2活動如今在數據填寫Add_Case2和滑動到Add_Case3活動,如果我現在回刷去Add_Case1Add_Case2 ,我可以看到我填寫的數據依然存在。但是當我現在再次從Add_Case1刷到Add_Case2Add_Case3。數據已經丟失。

我嘗試使用android:launchMode="singleTop",android:launchMode="singleTask",即使onSaveInstanceState,但它不起作用。
下面是文件

相關代碼Add_Case1

  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_Case2

 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() { 

     } 

     } 

Add_Case3

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

     } 

清單

<activity 
     android:name="app.schedulawyer.com.Add_case1" 
     android:label="Add Case" 
     android:screenOrientation="portrait" 
     android:launchMode="singleTop" > 
     <intent-filter> 
      <action android:name="app.schedulawyer.com.Add_case1" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name="app.schedulawyer.com.Add_case2" 
     android:label="Add Case" 
     android:screenOrientation="portrait" 
     android:launchMode="singleTop" > 
     <intent-filter> 
      <action android:name="app.schedulawyer.com.Add_case2" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name="app.schedulawyer.com.Add_case3" 
     android:label="Add Case" 
     android:screenOrientation="portrait" 
     android:launchMode="singleTop" > 
     <intent-filter> 
      <action android:name="app.schedulawyer.com.Add_case3" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 

回答

3

我認爲你應該改變你完全這樣做的方式。如果你真的想使用Activity的話,你可以將數據保存到磁盤或數據庫,或者它是小的SharedPreferences。這樣你就可以隨時獲得數據。

但我認爲你應該在ViewPager使用它1 Activity,1 ViewPager和3 Fragments

您可以在下面找到一個教程: http://developer.android.com/training/animation/screen-slide.html

而且你還可以得到代碼準備好,當你打開一個新的活動只使用「新建空白活動」,然後選擇「刷卡次數+標題地帶」作爲導航類型。之後,您將具有上述描述的行爲,然後如果需要,可以刪除標題條。

+0

幫助很大找到一些很好的教程。 請看看你是否也可以幫助我。 http://stackoverflow.com/q/22182367/3377639 –