2011-09-23 92 views
3

我正在製作一種社交網絡應用程序。我正在實現註銷功能。 在退出按鈕單擊它應該導航到登錄界面,而是它現在導航到主頁screen.I正在使用註銷下面的代碼..Android中的註銷功能

IntentFilter intentFilter = new IntentFilter(); 
    intentFilter.addAction("com.package.ACTION_LOGOUT"); 
    registerReceiver(new BroadcastReceiver() { 
    public void onReceive(Context context, Intent intent) { 

     System.out.println("onReceive Log out in progress"); 
      Intent intent1 = new Intent(getApplicationContext(),   Login.class); 
        startActivity(intent1); 
           finish(); 
       } 
      }, intentFilter); 

回答

4

,只需提供意圖您登錄活動,並把意圖標誌

inten1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

它將清除所有活動並導航到您的登錄頁面。

+0

我已經做到了already.it再次導航主頁instaed的登錄屏幕 – ekjyot

+0

這樣,你就完成所有活動,同時導航到旁邊的活動,你需要檢查你的代碼,在開始活動時查看是否有任何完成()行。 –

+0

雅我正在完成活動 – ekjyot

1

這是一個堆棧問題。你需要處理它。我發現的最佳解決方案是,當您的應用程序運行時,在單個活動堆棧中保持堆疊狀態,只有登錄屏幕將堆疊,如果用戶按下後退按鈕,則會看到主屏幕。

+0

如何做到這一點? – ekjyot

+0

完成()活動,當你移動到新的活動...... –

6

使用以下注銷。

yourintent.setflag(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK); 

它可以幫助你

+0

我也試過這也......但它又是導航到主頁屏幕 – ekjyot

+0

嗨ekjyot當你從主頁活動向前移動完成你的主頁。 – amity

+0

嗨ekjyot我已經更新了我的答案請嘗試一下,我相信它的工作 – amity

1

什麼工作對我來說是跟蹤您的登錄狀態的內部,使用某種全球:

public boolean loggedin = false; 

,然後在所有的活動,覆蓋的onResume ()和完成()如果您已註銷:

@Override 
public void onResume() { 
    super.onResume(); 
    if (!loggedin) 
    finish(); 
} 
0

首先對這些代碼進行更改

Intent intent = new Intent(getApplicationContext(),Login.class); 
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(intent); 

然後刪除finish();寫入您的廣播接收器。 祝你好運。

1

試試這個:

Intent intent = new Intent(getApplicationContext(), Login.class); 
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
Toast.makeText(this, "Signed out", Toast.LENGTH_SHORT).show(); 
startActivity(intent); 
finish();