2017-02-10 35 views
-2

嘿傢伙如何在我的項目中使用sharedpreferences。我在主要活動和第二活動中有一個浮動按鈕,當我單擊浮動按鈕時,指南針將被禁用,然後當我進入第二個活動時,指南針被啓用,而禁用改爲。如何解決此問題?如何通過查看活動之間的可見性狀態

這裏是我的代碼baseactivity

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 



    FloatingActionButton compass1 = (FloatingActionButton)findViewById(R.id.comp); 
    compass1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if(findViewById(R.id.relativeLayout1).getVisibility()==View.VISIBLE){ 
       findViewById(R.id.relativeLayout1).setVisibility(View.GONE); 
       Toast.makeText(getApplicationContext(),"Compass Has Been Disabled",Toast.LENGTH_SHORT).show(); 
      } 

      else{ 

       findViewById(R.id.relativeLayout1).setVisibility(View.VISIBLE); 
       Toast.makeText(getApplicationContext(),"Compass Has Been Enabled",Toast.LENGTH_SHORT).show(); 
      } 
     } 

    }); 

    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 navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 



} 

@覆蓋 公共布爾onNavigationItemSelected(菜單項項){

// Handle navigation view item clicks here. 
    int id = item.getItemId(); 

    if (id == R.id.nav_first) { 
     Intent myIntent = new Intent(this,MainActivity.class); 
     myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     startActivity(myIntent); 
     finish(); 
    } else if (id == R.id.nav_second) { 
     Intent myIntent = new Intent(this,SecondFloor.class); 
     myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     startActivity(myIntent); 
     finish(); 



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

回答

1

你不需要SharedPreferences了點。只需傳遞一個具有指南針可見性狀態的Intent extra。

保存從compass1的點擊監聽器的狀態在一類領域isCompassVisible

當創建SecondFloor意圖做:

Intent myIntent = new Intent(this,SecondFloor.class); 
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
myIntent.putExtra("isCompassVisible", isCompassVisible); 
startActivity(myIntent); 

SecondFloor活動的onCreate(或任何你需要它):

if(getIntent().hasExtra("isComapssVisible") 
    comapssView.setVisibility(getIntent().getBooleanExtra("isCompassVisible", true) ? View.VISIBLE : View.GONE); 
+0

嘿我在頂部添加我的代碼 – madara

+0

我不知道如何在類字段isCompassVisible中保存從compass1點擊監聽器的狀態。對不起,即時通訊新手在android – madara

+0

'isCompassVisible = findViewById(R.id.relativeLayout1).getVisibility()== View.VISIBLE? false:true;' –

0

1.您可以使用Shared preferences將值設置爲:

SharedPreferences sharedPreferences = getSharedPreferences(Values.SHARED_PREF_NAME, Context.MODE_PRIVATE); 
SharedPreferences.Editor editor = sharedPreferences.edit(); 
editor.putBoolean("isShowing", true/false); 
editor.apply(); 

,並把它作爲:

SharedPreferences prefs = ((Activity)context).getSharedPreferences("TAG", Context.MODE_PRIVATE); 
Boolean shown = prefs.getBoolean("isShowing",true); 
  • 但是,你會傳遞布爾值在你的意圖更好。
  • 簡單地將它傳遞:

    Intent intent = new Intent(FirstActivity.this, SecondActivity.class); 
    Bundle bundle = new Bundle(); 
    bundle.putBoolean("isShowing",true/false); 
    intent.putExtras(bundle); 
    startActivity(intent); 
    

    ,並得到它:

    Boolean b = getIntent().getBooleanExtra("isShowing",false); 
    
  • 或者,您也可以在使用公共靜態布爾變量可以從任何活動中訪問您的活動或utils文件夾。
  • +0

    不需要創建'Bundle'對象,但除此之外你是對的。 –

    相關問題