2017-08-15 72 views
1

当我使用共享首选项类作为分离的类时,我将检查用户是否已登录或不在导航活动类中,如果是第一次它转移我的登录活动,否则继续在导航活动如何在我处于另一个活动时完成活动

但我的问题是,当我要回backPressed登录活动它应该关闭应用程序,但它转移我在导航活动.... pls帮我...

package com.apkglobal.no_dues; 

import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 

public class Shared { 
    SharedPreferences sp; 
    SharedPreferences.Editor ed; 
    String Filename="himanshu"; 
    int mode=0; 
    Context context; 
    String First="first"; 


public Shared(Context context) { 
    this.context = context; 
    sp=context.getSharedPreferences(Filename,mode); 
    ed=sp.edit(); 
} 


public void secondtime() 
{ 
    ed.putBoolean(First,true); 
    ed.commit(); 
} 

public boolean firsttime() 
{ 
    if(!this.isfirst()) 
    { 
     Intent i=new Intent(context,LoginActivity.class); 
     i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 
     i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(i); 

    } 
    return false; 
} 

public boolean isfirst() { 
    return sp.getBoolean(First,false); 
} 

public void getback() 
{ 
    ed.putBoolean(First,false); 
    ed.commit(); 
} 


} 

Navigationacitivity.class

package com.apkglobal.no_dues; 

import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v7.app.AlertDialog; 
import android.view.View; 
import android.support.design.widget.NavigationView; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Button; 
import android.widget.TextView; 

import com.apkglobal.no_dues.Fragment.AboutFragment; 
import com.apkglobal.no_dues.Fragment.BookDetailsFragment; 
import com.apkglobal.no_dues.Fragment.FeedbackFragment; 
import com.apkglobal.no_dues.Fragment.HomeFragment; 
import com.apkglobal.no_dues.Fragment.MarksFragment; 
import com.apkglobal.no_dues.Fragment.NoDuesFormFragment; 

public class NavigationActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener{ 
    Button btn_mis,btn_attendence; 
    Shared shared; 
    String rollno; 
    SharedPreferences sp; 
    SharedPreferences.Editor ed; 

    NavigationView navigationView; 

    TextView tv_rollno; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     shared = new Shared(getApplicationContext()); 
     shared.firsttime(); 

     setContentView(R.layout.activity_navigation); 

     btn_attendence = (Button) findViewById(R.id.btn_attendence); 
     btn_mis=(Button)findViewById(R.id.btn_mis); 
     tv_rollno = (TextView)findViewById(R.id.tv_rollno); 

     navigationView = (NavigationView) findViewById(R.id.nav_view); 

     View header=navigationView.getHeaderView(0); 

     tv_rollno = (TextView)header.findViewById(R.id.tv_rollno); 



     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 





     sp=getSharedPreferences("rajput",MODE_PRIVATE); 
     rollno=sp.getString("rollno",null); 


     tv_rollno.setText(rollno); 





     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

       navigationView.setNavigationItemSelectedListener(this); 




     defaultSelectitem(R.id.nav_homepage); 



    } 






    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 

     } 

     else { 


      AlertDialog.Builder ab = new AlertDialog.Builder(NavigationActivity.this); 
      ab.setIcon(R.drawable.logo); 
      ab.setTitle("Exit Application"); 
      ab.setMessage("Are You Sure ?"); 
      ab.setCancelable(false); 
      ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        finish(); 
       } 
      }); 
      ab.setNegativeButton("No", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.dismiss(); 
       } 
      }); 
      ab.create(); 
      ab.show(); 
     } 


    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.navigation, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      Intent i = new Intent(getApplicationContext(), SettingActivity.class); 
      startActivity(i); 
     } else if (id == R.id.action_contact) { 
      Intent i = new Intent(getApplicationContext(), ContactActivity.class); 
      startActivity(i); 
     } else if (id == R.id.action_search) { 

     } 

     return super.onOptionsItemSelected(item); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     defaultSelectitem(item.getItemId()); 


     return true; 
    } 


    private void defaultSelectitem(int itemId) { 

     Fragment fragment = null; 

     switch (itemId) { 
      case R.id.nav_homepage: 
       fragment = new HomeFragment(); 
       break; 

      case R.id.nav_nodues_form: 
       fragment = new NoDuesFormFragment(); 
       break; 

      case R.id.nav_feedback: 
       fragment = new FeedbackFragment(); 
       break; 

      case R.id.nav_marks: 
       fragment = new MarksFragment(); 
       break; 

      case R.id.nav_book: 
       fragment = new BookDetailsFragment(); 
       break; 

      case R.id.nav_about: 
       fragment = new AboutFragment(); 
       break; 

      case R.id.nav_fees_detail: 

          Intent intent = new Intent(NavigationActivity.this,FeesDetail.class); 
      startActivity(intent); 


       break; 

      case R.id.nav_share: 

       break; 

      case R.id.nav_logout: 
       shared.getback(); 
       Intent i = new Intent(getApplicationContext(), LoginActivity.class); 
       startActivity(i); 

       finish(); 

       break; 
     } 

     if (fragment != null) { 
      FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction().replace(R.id.frame, fragment); 
      fragmentTransaction.commit(); 

     } 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 

    } 


} 
+0

的可能的复制[安卓:不保存到堆栈开放活动(https://stackoverflow.com/questions/12358485/android-open-activity -without保存成 - 在堆栈) –

回答

1

你必须采取活动作为PARAM而不是背景。然后在开始活动后,您应完成当前活动。

你的代码应该是这样的。

package com.apkglobal.no_dues; 

import android.content.Intent; 
import android.content.SharedPreferences; 

public class Shared { 
    SharedPreferences sp; 
    SharedPreferences.Editor ed; 
    String Filename="himanshu"; 
    int mode=0; 
    Activity activity; 
    String First="first"; 


public Shared(Activity activity) { 
    this.activity= context; 
    sp=activity.getSharedPreferences(Filename,mode); 
    ed=sp.edit(); 
} 


public void secondtime() 
{ 
    ed.putBoolean(First,true); 
    ed.commit(); 
} 

public boolean firsttime() 
{ 
    if(!this.isfirst()) 
    { 
     Intent i=new Intent(activity,LoginActivity.class); 
     i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 
     i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     activity.startActivity(i); 
     activity.finish(); 

    } 
    return false; 
} 

public boolean isfirst() { 
    return sp.getBoolean(First,false); 
} 

public void getback() 
{ 
    ed.putBoolean(First,false); 
    ed.commit(); 
} 


} 

希望它能帮助:)

+0

感谢兄弟....其工作非常感谢 –

+0

标记为正确的答案。这对其他人会有所帮助。 –

+0

yaa sure ....... –

1

Manifest.xml添加android:noHistory=trueLoginActivity。因此,登录活动不会在后台。

+0

我尝试,但它不工作 –

1

只需添加finish()完成当前活动:

if(!this.isfirst()){ 
    Intent i=new Intent(context,LoginActivity.class); 
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(i); 
    finish() 
} 
+0

其产生错误的结束词 公共布尔firsttime(){ 如果 (!this.isfirst()){ 意向 我= new Intent(context,LoginActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); finish(); } return false; } –

0
use NavigationActivity.this instead of getApplicationContext() 

shared = new Shared(NavigationActivity.this); 
shared.firsttime(); 

and call finish(); method after startActivity(); 
相关问题