2017-02-10 37 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'对象,但除此之外你是对的。 –

    相关问题