简体   繁体   中英

Correct way to share a View between Android activities?

I want to be able to share a View between different Android activities. This view is a music player that I want to always be at the footer of every activity. I also want to be able to access it from any class, so I have it referenced statically from my MainActivity. The view is called Player .

My MainActivity will set it up....

public class MainActivity extends Activity{
public static Player player;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    MainActivity.player = new Player(this);
}

The Player class is made by inflating my player.xml file.

public class Player extends LinearLayout{
private ImageView previousButton, playButton, nextButton, playlistButton;
private TextView songTitle;

public Player(Context context)
{
    super(context);
    init(context);

}


private void init(Context context)
{
    LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = layoutInflater.inflate(R.layout.player, this);

    this.previousButton = (ImageView) view.findViewById(R.id.playerPreviousButton);
    this.playButton = (ImageView) view.findViewById(R.id.playerPlayButton);
    this.nextButton = (ImageView) view.findViewById(R.id.playerNextButton);
    this.playlistButton = (ImageView) view.findViewById(R.id.playerPlaylistButton);
    this.songTitle = (TextView) view.findViewById(R.id.playerSongTitle);

}

How can I share this across multiple activities? I have a lot of functions in my Player class that I did not list that I need to be able to access from any class, not just the activity classes, thus I though of going the static route and having it only initialized once.

Could someone help me in letting me know the correct way to go about doing this?

If you only want to initialize your Player UI once, consider using Fragments instead of Activities. You would be able to swap out various UIs in the top portion of your screen, while keeping your PlayerFragment active and untouched on the bottom. This would also mean you do not have to bind/unbind your UI to your audio service every time the user navigates to a different part of your application.

For your concern you need to define a separate XML layout for that.

Now when you need to show that XML in another layout you may try like this ...

<include layout="@layout/your_music_layout"/>

Simply adjust at place where exactly you need this.

Secondly for accessing it's feature you should declare SUPER CLASS. This super class will be extended by all other classes. Some thing like this ..

Public class MainActivity extends SuperClass{

}

Here your super class will extend Activity which indirectly will be extended by rest of all classes in your app.

That's it. you are good to go with this.

Correct way is not to share a View across multiple activities. Each view belongs to one activity and you shouldn't try to share it. Each view is constructed by passing a Context to it and it's tied to this Context .

You can create a parent class for all your activities and put the necessary code in there. This way you'll be able to share the code but still create separate View objects.

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