2017-06-20 45 views
1

我有兩頁1.MainActivity 2.下一頁。我每10秒鐘刷新一次下一頁。問題是一旦我點擊回到主活動頁面它再次進入下一頁,因爲它會刷新。我不希望它直接進入下一頁。我嘗試過使用onBackPressed按鈕,但它不起作用。 以下是我的代碼: -停止刷新頁面,一旦我們退出

MainActivity.java

public class MainActivity extends Activity { 
    Button next; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     next=(Button)findViewById(R.id.refresh); 
     next.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent=new Intent(MainActivity.this,Next.class); 
       startActivity(intent); 

      } 
     }); 
    } 
} 

NextPage.java

public class Next extends Activity { 
    ImageView iv; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.next); 
     iv=(ImageView)findViewById(R.id.imagev); 

     Toast.makeText(Next.this, "Refreshed", Toast.LENGTH_SHORT).show(); 



     final Handler handler = new Handler(); 
     handler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 

       Intent intent = getIntent(); 
       overridePendingTransition(0, 0); 
       intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
       finish(); 
       overridePendingTransition(0, 0); 
       startActivity(intent); 
      } 
     }, 10000); 




    } 

    @Override 
    public void onBackPressed() { 
     super.onBackPressed(); 
     finish(); 
    } 

} 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.ram.refresh.MainActivity"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Hello World!" 
     android:id="@+id/hello"/> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/refresh" 
     android:text="Next page" 
     android:layout_below="@+id/hello"/> 
</RelativeLayout> 

next.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/imagev" 
     android:src="@drawable/smiley"/> 

</LinearLayout> 
+0

簡單,標準的解決方案創建一個使用數組列表活動的堆棧。 [這裏是解決方案](https://stackoverflow.com/questions/38326642/how-to-remove-a-particular-activity-from-android-back-stack/38326848#38326848) –

回答

3
@Override 
     public void onBackPressed() { 
handler.removeCallbacksAndMessages(null); 
      finish(); 
     } 

只有在onBackPressed完成(),刪除super.onBackPressed();嘗試&讓我知道

+0

沒有它繼續下一步頁。 – riya

+0

使您的處理程序在活動中全球化,並嘗試更新的答案。 – Moulesh

+0

感謝它現在工作。 :) – riya