2

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.

alexistkd
  • 906
  • 2
  • 14
  • 34

2 Answers2

3

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

Neto Marin
  • 674
  • 3
  • 15
  • if i use a service can i make when user press back button tu have the activity in background too? thats my goal something like tunein radio when u open a stream if u press back button it stays in background the music then if client want to stop it need to go to the app options menu and press exit item – alexistkd Oct 11 '12 at 22:18
  • 2
    You can send an Intent to the service and so, control the actions of your player. The activity is not a background class. You should study the life cycle of an Activity: http://developer.android.com/reference/android/app/Activity.html 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. – Neto Marin Oct 11 '12 at 22:21
  • 1
    @Neto Martin -Thats a really helpful comment, you should edit your answer to include it – kmb64 Oct 11 '12 at 22:43
0

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.

dorjeduck
  • 7,624
  • 11
  • 52
  • 66