alexistkd
alexistkd

Reputation: 898

How to void application exit when press hardware back button android?

how can i avoid the exit of my activity when i press the back button of the device, for example i have this flow in my app:

Main layout then when i click one item in the listview im going to Secondactivity but when i press back button my secondactivity means that my music streams stops

its actually an aac player play streams live.

this is actually part of my Secondactivity code:

@Override
    protected void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );

        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

        setContentView( R.layout.main2 );

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);

        title = (TextView) findViewById(R.id.title);

        icon  = (ImageView) findViewById(R.id.icon);

        logo_image = (ImageView) findViewById(R.id.logo_image);

thanks.

Upvotes: 2

Views: 910

Answers (2)

Neto Marin
Neto Marin

Reputation: 674

Don't mess with the back button behavior. In this case, you have to use a service to play and control your music. And stop the service when you want to stop the music.

You can send an Intent to the service and so, control the actions of your player. The activity is not a background class. Your Activity is the view of your application, so, if user want to leave the activity will be killed. So, to keep the sound playing (like a player) you have to user the service.

[]s Neto

Upvotes: 3

dorjeduck
dorjeduck

Reputation: 7794

You can override the

void onBackPressed()

method of the Activity class yet as Neto already hinted you might not make your user happy with this.

Upvotes: 0

Related Questions