Mohit Patel
Mohit Patel

Reputation: 29

Action Bar back arrow not working onOptionsItemSelected

Back Arrow in Action Bar is not working, I want to go to MainActivity on pressing back arrow, but it's not showing any error and neither it's taking me back. I did tried it by calling ParentingActivity in Manifest xml, it works fine there, but by doing that way when I press back arrow my music player still plays song. I want to finish current activity when back arrow is pressed. Below is the code;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.os.Handler;


public class Datadetail extends AppCompatActivity implements OnSeekBarChangeListener{

TextView txtname;
TextView txtaarti;
String[] itemname;
int position;
int resID;
private SeekBar seekbar;
ImageButton buttonPlay;
private MediaPlayer mplayer;
private Handler mHandler = new Handler();
private Utilities utils;
private TextView songCurrentDurationLabel;
private TextView songTotalDurationLabel;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.data_detail);

    Intent i = getIntent();

    position = i.getExtras().getInt("position");
    itemname = i.getStringArrayExtra("itemname");

    txtname = (TextView) findViewById(R.id.textView2);
    txtname.setText(itemname[position]);

    txtaarti = (TextView) findViewById(R.id.textView3);
    txtaarti.setText(Aarti.aartitxt[position]);

    seekbar = (SeekBar) findViewById(R.id.seekBar);
    seekbar.setOnSeekBarChangeListener(this);
    seekbar.setEnabled(true);

    songCurrentDurationLabel = (TextView) findViewById(R.id.songCurrentDurationLabel);
    songTotalDurationLabel = (TextView) findViewById(R.id.songTotalDurationLabel);
    utils = new Utilities();
    mplayer = new MediaPlayer();

    android.support.v7.app.ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);

    buttonPlay = (ImageButton) findViewById(R.id.imageButton);

    playsong(position);

    buttonPlay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mplayer.isPlaying()) {
                if (mplayer != null) {
                    mplayer.pause();
                    buttonPlay.setImageResource(R.mipmap.ic_play);
                }
            } else {
                if (mplayer != null) {
                    mplayer.setOnCompletionListener(completionListener);
                    mplayer.start();
                    buttonPlay.setImageResource(R.mipmap.ic_pause);

                }
            }

        }
    });


}

public void playsong(int position) {
    switch (position){
        case 0:
            resID = R.raw.ganesh_aarti;
            break;
        case 1:
            resID = R.raw.durga_aarti;
            break;
    }
    if (mplayer !=null){
        mplayer.release();
    }
    mplayer = MediaPlayer.create(this, resID);
    buttonPlay.setImageResource(R.mipmap.ic_play);
    seekbar.setProgress(0);
    seekbar.setMax(100);
    updateProgressBar();

}

public void updateProgressBar() {
    mHandler.postDelayed(mUpdateTimeTask,100);
}

private Runnable mUpdateTimeTask = new Runnable() {
    public void run() {
        int totalDuration = mplayer.getDuration();
        int currentDuration = mplayer.getCurrentPosition();

        // Displaying Total Duration time
       songTotalDurationLabel.setText(""+utils.milliSecondsToTimer(totalDuration));
        // Displaying time completed playing
       songCurrentDurationLabel.setText(""+utils.milliSecondsToTimer(currentDuration));

        // Updating progress bar
        int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration));
        //Log.d("Progress", ""+progress);
        seekbar.setProgress(progress);

        // Running this thread after 100 milliseconds
        mHandler.postDelayed(this, 100);
    }
};

@Override
public void onBackPressed() {
    if (mplayer.isPlaying()) {
        mplayer.stop();
    }
    mHandler.removeCallbacks(mUpdateTimeTask);
    mplayer.release();
    super.onBackPressed();
}

public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) {

}

@Override
public void onStartTrackingTouch(SeekBar seekbar) {
    // TODO Auto-generated method stub
    mHandler.removeCallbacks(mUpdateTimeTask);

}

@Override
public void onStopTrackingTouch(SeekBar seekbar) {
    // TODO Auto-generated method stub
    mHandler.removeCallbacks(mUpdateTimeTask);
    int totalDuration = mplayer.getDuration();
    int currentPosition = utils.progressToTimer(seekbar.getProgress(), totalDuration);

    // forward or backward to certain seconds
    mplayer.seekTo(currentPosition);

    // update timer progress again
    updateProgressBar();

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()){
        case R.id.home:
            Intent parentActivityIntent = new Intent(this, MainActivity.class);
            parentActivityIntent.addFlags(
                    Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(parentActivityIntent);
            finish();
            return true;

        case  R.id.action_name:
            Intent intent=new Intent(android.content.Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
            intent.putExtra(android.content.Intent.EXTRA_TEXT, Aarti.aartitxt[position]);
            startActivity(Intent.createChooser(intent, "How do you want to share?"));
           return true;

    }
    return super.onOptionsItemSelected(item);

}

MediaPlayer.OnCompletionListener completionListener = new MediaPlayer.OnCompletionListener() {

    @Override
    public void onCompletion(MediaPlayer mp) {

        mplayer.seekTo(0);
        buttonPlay.setImageResource(R.mipmap.ic_play);

    }
};

}

Upvotes: 0

Views: 125

Answers (1)

user3600801
user3600801

Reputation:

use case android.R.id.home: instead of case R.id.home:

Upvotes: 1

Related Questions