简体   繁体   中英

How to open an activity on top of the main activity layout using intent?

I want to create an intent in MainActivity that will launch CompassActivity.

Both classes share a common layout activity_main, but use different parts of it.

Here is the intent I currently have in MainActivity which is supposed to open the CompassActivity.java class file but doesn't.

public void startCompass(View v)
    {
      Intent intent = new Intent(this, CompassActivity.class);
      startActivity(intent);
    }

What I have tried:

  • Altering androidmainfest.xml.
  • Changing the intent itself.
  • Changing the SDK version.

您所要做的就是将正确的变量放在onCreate方法下,它应该可以正常工作。

Do you want to open an intent or do you just want to show/hide the different views you are using?

Instantiate your Views (findViewById) in your onCreate, then you can call view.setVisibility(View.GONE or View.VISIBILE) to hide or display your different views.

Bottom line, your views exist already on the main Activity (so no Intent needed to start another Activity) - you just need to control which views are visible at a particular time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM