簡體   English   中英

在刷卡活動和維護數據之間切換

[英]Switching between swiping activities and maintaining data

我有3個活動Add_Case1,Add_Case2和Add_Case3。 我正在使用“滑動手勢”在它們之間來回切換。

我的問題是:我在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>

我認為您應該完全更改此操作的方式。 如果您確實要使用Activity,則可以將數據保存到磁盤或數據庫中,或者如果它是較小的SharedPreferences。 這樣,您可以隨時獲取數據。

但我認為您應該在其中使用1個Activity ,1個ViewPager和該ViewPager 3個Fragments

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

打開新活動時,也可以准備好代碼,只需使用“新建空白活動”,然后選擇“滑動視圖+標題欄”作為導航類型。 之后,您將具有我上面描述的行為,然后可以刪除標題欄。

是的@tasomaniac是正確的,使用view pager是更好的主意。 http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/中找到一些不錯的教程

https://github.com/JakeWharton/Android-ViewPagerIndicator

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM