我希望当我在SongPlayingFragment中按下硬件后退按钮时,我应该返回主屏幕。我该怎么做?

时间:2018-07-03 12:00:17

标签: android android-fragments kotlin

package com.shivamkapila.echo.fragments
import android.app.Activity
import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.media.AudioManager
import android.media.MediaPlayer
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.support.v4.app.Fragment
import android.support.v4.content.ContextCompat
import android.util.Log
import android.view.*
import android.widget.ImageButton
import android.widget.SeekBar
import android.widget.TextView
import android.widget.Toast
import com.cleveroad.audiovisualization.AudioVisualization
import com.cleveroad.audiovisualization.DbmHandler
import com.cleveroad.audiovisualization.GLAudioVisualizationView
import com.shivamkapila.echo.CurrentSongHelper
import com.shivamkapila.echo.R
import com.shivamkapila.echo.Songs
import com.shivamkapila.echo.activities.MainActivity
import com.shivamkapila.echo.databases.EchoDatabase
import com.shivamkapila.echo.utils.SeekBarController
import java.util.*
import java.util.concurrent.TimeUnit

class SongPlayingFragment : Fragment() {

object Statified {

    var myActivity: Activity? = null
    var mediaPlayer: MediaPlayer? = null
    var startTimeNext: TextView? = null
    var endTimeNext: TextView? = null
    var playPauseImageButton: ImageButton? = null
    var previousImageButton: ImageButton? = null
    var nextImageButton: ImageButton? = null
    var loopImageButton: ImageButton? = null
    var seekbar: SeekBar? = null
    var songArtistView: TextView? = null
    var songTitleView: TextView? = null
    var shuffleImageButton: ImageButton? = null
    var check: Boolean = true
    var _currentPosition: Int = 0
    var fetchSongs: ArrayList<Songs>? = null

    var currentSongHelper: CurrentSongHelper? = null

    var audioVisualization: AudioVisualization? = null
    var glView: GLAudioVisualizationView? = null
    var fab: ImageButton? = null

    var favoriteContent: EchoDatabase? = null
    var counter: Int = 0
    var mSensorManager: SensorManager? = null
    var mSensorListener: SensorEventListener? = null
    var MY_PREFS_NAME = "ShakeFeature"
    var back: String? = null
    var updateSongTime = object : Runnable {
        override fun run() {
            try {
                val getCurrent = Statified.mediaPlayer?.getCurrentPosition()
                startTimeNext?.setText(String.format("%02d:%02d",
                        TimeUnit.MILLISECONDS.toMinutes(getCurrent?.toLong() as Long),
                        TimeUnit.MILLISECONDS.toSeconds(getCurrent?.toLong() as Long) -
                                TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(getCurrent?.toLong() as Long))))

                seekbar?.setProgress(getCurrent?.toInt() as Int)
                Statified.check = true
                Handler().postDelayed(this, 1000)

            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

}


object Staticated {

    var MY_PREFS_SHUFFLE = "Shuffle feature"
    var MY_PREFS_LOOP = "Loop feature"

    fun onSongComplete() {

        if (Statified.currentSongHelper?.isShuffle as Boolean) {
            playNext("PlayNextLikeNormalShuffle")
            Statified.currentSongHelper?.isPlaying = true

        } else {
            if (Statified.currentSongHelper?.isLoop as Boolean) {

                Statified.currentSongHelper?.isPlaying = true

                var nextSong = Statified.fetchSongs?.get(Statified._currentPosition)
                Statified.currentSongHelper?.songPath = nextSong?.songData
                Statified.currentSongHelper?.songTitle = nextSong?.songTitle
                Statified.currentSongHelper?.songArtist = nextSong?.artist
                Statified.currentSongHelper?.songId = nextSong?.songID as Long
                Statified.currentSongHelper?.currentPosition = Statified._currentPosition

                updateTextViews(Statified.currentSongHelper?.songTitle as String, Statified.currentSongHelper?.songArtist as String)

                Statified.mediaPlayer?.reset()
                try {
                    Statified.mediaPlayer?.setDataSource(Statified.myActivity, Uri.parse(Statified.currentSongHelper?.songPath) as Uri)
                    Statified.mediaPlayer?.prepare()
                    Statified.mediaPlayer?.start()
                    processInformation(Statified.mediaPlayer as MediaPlayer)
                } catch (e: Exception) {
                    e.printStackTrace()
                }

            } else {
                playNext("PlayNextNormal")
                Statified.currentSongHelper?.isPlaying = true

            }

        }

        if (Statified.favoriteContent?.checkifIdExists(Statified.currentSongHelper?.songId?.toInt() as Int) as Boolean) {
            Statified.fab?.setImageDrawable(ContextCompat.getDrawable(Statified.myActivity, R.drawable.favorite_on))
        } else {
            Statified.fab?.setImageDrawable(ContextCompat.getDrawable(Statified.myActivity, R.drawable.favorite_off))

        }
    }

    fun updateTextViews(songTitle: String, songArtist: String) {
        var songTitleUpdated = songTitle
        var songArtistUpdated = songArtist
        if (songTitle.equals("<unknown>", true)) {
            songTitleUpdated = "unknown"
        }
        if (songArtist.equals("<unknown>", true)) {
            songArtistUpdated = "unknown"
        }
        Statified.songTitleView?.setText(songTitleUpdated)
        Statified.songArtistView?.setText(songArtistUpdated)
    }


    fun processInformation(mediaPlayer: MediaPlayer) {
        val finalTime = mediaPlayer.duration
        val startTime = mediaPlayer.currentPosition
        Statified.seekbar?.max = finalTime
        Statified.startTimeNext?.setText(String.format("%02d:%02d",
                TimeUnit.MILLISECONDS.toMinutes(startTime.toLong()),
                TimeUnit.MILLISECONDS.toSeconds(startTime.toLong()) -
                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(startTime.toLong()))))

        Statified.endTimeNext?.setText(String.format("%02d:%02d",
                TimeUnit.MILLISECONDS.toMinutes(finalTime.toLong()),
                TimeUnit.MILLISECONDS.toSeconds(finalTime.toLong()) -
                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(finalTime.toLong()))))

        Statified.seekbar?.setProgress(startTime)
        Handler().postDelayed(Statified.updateSongTime, 1000)

    }


    fun playNext(check: String) {

        if (check.equals("PlayNextNormal", true)) {
            Statified._currentPosition = Statified._currentPosition + 1
        } else if (check.equals("PlayNextLikeNormalShuffle", true)) {
            var randomObject = Random()
            var randomPosition = randomObject.nextInt(Statified.fetchSongs?.size?.plus(1) as Int)
            Statified._currentPosition = randomPosition

        }
        if (Statified._currentPosition == Statified.fetchSongs?.size) {
            Statified._currentPosition = 0
        }
        Statified.currentSongHelper?.isLoop = false
        var nextSong = Statified.fetchSongs?.get(Statified._currentPosition)
        Statified.currentSongHelper?.songPath = nextSong?.songData
        Statified.currentSongHelper?.songTitle = nextSong?.songTitle
        Statified.currentSongHelper?.songArtist = nextSong?.artist
        Statified.currentSongHelper?.songId = nextSong?.songID as Long
        Statified.currentSongHelper?.currentPosition = Statified._currentPosition
        var editorLoop = Statified.myActivity?.getSharedPreferences(Staticated.MY_PREFS_LOOP, Context.MODE_PRIVATE)?.edit()

        Statified.currentSongHelper?.isLoop = false
        Statified.loopImageButton?.setBackgroundResource(R.drawable.loop_white_icon)
        editorLoop?.putBoolean("feature", false)
        editorLoop?.apply()
        if (Statified.currentSongHelper?.isPlaying as Boolean) {
            Statified.playPauseImageButton?.setBackgroundResource(R.drawable.pause_icon)
        } else {
            Statified.playPauseImageButton?.setBackgroundResource(R.drawable.play_icon)
        }
        updateTextViews(Statified.currentSongHelper?.songTitle as String, Statified.currentSongHelper?.songArtist as String)

        Statified.mediaPlayer?.reset()
        try {
            Statified.mediaPlayer?.setDataSource(Statified.myActivity, Uri.parse(Statified.currentSongHelper?.songPath) as Uri)
            Statified.mediaPlayer?.prepare()
            Statified.mediaPlayer?.start()
            Staticated.processInformation(Statified.mediaPlayer as MediaPlayer)
            processInformation(Statified.mediaPlayer as MediaPlayer)
        } catch (e: Exception) {
            e.printStackTrace()
        }

        if (Statified.favoriteContent?.checkifIdExists(Statified.currentSongHelper?.songId?.toInt() as Int) as Boolean) {
            Statified.fab?.setImageDrawable(ContextCompat.getDrawable(Statified.myActivity, R.drawable.favorite_on))
        } else {
            Statified.fab?.setImageDrawable(ContextCompat.getDrawable(Statified.myActivity, R.drawable.favorite_off))
        }

    }

    fun playPrevious() {

        Statified._currentPosition = Statified._currentPosition - 1
        if (Statified._currentPosition == -1) {
            Statified._currentPosition = 0
        }
        if (Statified.currentSongHelper?.isPlaying as Boolean) {
            Statified.playPauseImageButton?.setBackgroundResource(R.drawable.pause_icon)
        } else {
            Statified.playPauseImageButton?.setBackgroundResource(R.drawable.play_icon)
        }

        Statified.currentSongHelper?.isLoop = false

        var nextSong = Statified.fetchSongs?.get(Statified._currentPosition)

        Statified.currentSongHelper?.songPath = nextSong?.songData
        Statified.currentSongHelper?.songTitle = nextSong?.songTitle
        Statified.currentSongHelper?.songArtist = nextSong?.artist
        Statified.currentSongHelper?.songId = nextSong?.songID as Long
        Statified.currentSongHelper?.currentPosition = Statified._currentPosition

        Staticated.updateTextViews(Statified.currentSongHelper?.songTitle as String, Statified.currentSongHelper?.songArtist as String)


        Statified.mediaPlayer?.reset()
        try {
            Statified.mediaPlayer?.setDataSource(Statified.myActivity, Uri.parse(Statified.currentSongHelper?.songPath) as Uri)
            Statified.mediaPlayer?.prepare()
            Statified.mediaPlayer?.start()
            Staticated.processInformation(Statified.mediaPlayer as MediaPlayer)
            processInformation(Statified.mediaPlayer as MediaPlayer)
        } catch (e: Exception) {
            e.printStackTrace()
        }

        if (Statified.favoriteContent?.checkifIdExists(Statified.currentSongHelper?.songId?.toInt() as Int) as Boolean) {
            Statified.fab?.setImageDrawable(ContextCompat.getDrawable(Statified.myActivity, R.drawable.favorite_on))
        } else {
            Statified.fab?.setImageDrawable(ContextCompat.getDrawable(Statified.myActivity, R.drawable.favorite_off))
        }
    }


}


var mAcceleration: Float = 0f
var mAccelerationCurrent: Float = 0f
var mAccelerationLast: Float = 0f


override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    // Inflate the layout for this fragment
    val view = inflater!!.inflate(R.layout.fragment_song_playing, container, false)

    setHasOptionsMenu(true)

    activity.title = "Now Playing"

    Statified.seekbar = view?.findViewById(R.id.seekBar)
    Statified.startTimeNext = view?.findViewById(R.id.startTime)
    Statified.endTimeNext = view?.findViewById(R.id.endTime)
    Statified.playPauseImageButton = view?.findViewById(R.id.playPauseButton)
    Statified.nextImageButton = view?.findViewById(R.id.nextButton)
    Statified.previousImageButton = view?.findViewById(R.id.previousButton)
    Statified.loopImageButton = view?.findViewById(R.id.loopButton)
    Statified.shuffleImageButton = view?.findViewById(R.id.shuffleButton)
    Statified.songArtistView = view?.findViewById(R.id.songArtist)
    Statified.songTitleView = view?.findViewById(R.id.songTitle)

    Statified.glView = view?.findViewById(R.id.visualizer_view)
    Statified.fab = view?.findViewById(R.id.favoriteIcon)
    Statified.fab?.alpha = 0.8f
    return view

}

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    Statified.audioVisualization = Statified.glView as AudioVisualization
}


override fun onAttach(context: Context?) {
    super.onAttach(context)
    Statified.myActivity = context as Activity

}

override fun onAttach(activity: Activity?) {
    super.onAttach(activity)
    Statified.myActivity = activity
}

override fun onResume() {
    super.onResume()
    Statified.audioVisualization?.onResume()
    Statified.mSensorManager?.registerListener(Statified.mSensorListener, Statified.mSensorManager?.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_NORMAL)

}

override fun onPause() {
    Statified.audioVisualization?.onPause()
    Statified.mSensorManager?.unregisterListener(Statified.mSensorListener)
    super.onPause()
}

override fun onDestroyView() {
    super.onDestroyView()
    Statified.audioVisualization?.release()

}

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)

    Statified.currentSongHelper = CurrentSongHelper()
    Statified.currentSongHelper?.isPlaying = true
    Statified.currentSongHelper?.isLoop = false
    Statified.currentSongHelper?.isShuffle = false
    Statified.favoriteContent = EchoDatabase(Statified.myActivity)

    var path: String? = null
    var _songTitle: String? = null
    var _songArtist: String? = null
    var songId: Long = 0
    var fromFavBottomBar: String? = null
    var fromMainScreenBottomBar: String? = null
    try {

        path = arguments.getString("path")
        _songTitle = arguments.getString("songTitle")
        _songArtist = arguments.getString("songArtist")
        songId = arguments.getInt("songId").toLong()
        Statified._currentPosition = arguments.getInt("songPosition")
        Statified.fetchSongs = arguments.getParcelableArrayList("songData")
        Statified.currentSongHelper?.songPath = path
        Statified.currentSongHelper?.songTitle = _songTitle
        Statified.currentSongHelper?.songArtist = _songArtist
        Statified.currentSongHelper?.songId = songId
        Statified.currentSongHelper?.currentPosition = Statified._currentPosition

        fromFavBottomBar = arguments.get("FavBottomBar") as? String
        fromMainScreenBottomBar = arguments.get("MainScreenBottomBar") as? String

        Staticated.updateTextViews(Statified.currentSongHelper?.songTitle as String, Statified.currentSongHelper?.songArtist as String)

    } catch (e: Exception) {
        e.printStackTrace()
    }

    if (fromFavBottomBar != null) {
        Statified.mediaPlayer = FavoriteFragment.Statified.mediaPlayer
        Staticated.processInformation(Statified.mediaPlayer as MediaPlayer)
    } else if (fromMainScreenBottomBar != null) {
        Statified.mediaPlayer = MainScreenFragment.Statified.mediaPlayer
        Staticated.processInformation(Statified.mediaPlayer as MediaPlayer)

    } else {
        Statified.mediaPlayer = MediaPlayer()
        Statified.mediaPlayer?.setAudioStreamType(AudioManager.STREAM_MUSIC)
        try {
            Statified.mediaPlayer?.setDataSource(Statified.myActivity, Uri.parse(path) as Uri)
            Statified.mediaPlayer?.prepare()
        } catch (e: Exception) {
            e.printStackTrace()
        }

        Statified.mediaPlayer?.start()
        Staticated.processInformation(Statified.mediaPlayer as MediaPlayer)

    }
    Staticated.processInformation(Statified.mediaPlayer as MediaPlayer)
    if (Statified.mediaPlayer?.isPlaying as Boolean) {
        Statified.playPauseImageButton?.setBackgroundResource(R.drawable.pause_icon)
    } else {
        Statified.playPauseImageButton?.setBackgroundResource(R.drawable.play_icon)
    }

    Statified.mediaPlayer?.setOnCompletionListener {
        Staticated.onSongComplete()
    }

    clickHandler()

    var visualizationHandler = DbmHandler.Factory.newVisualizerHandler(Statified.myActivity as Context, 0)
    Statified.audioVisualization?.linkTo(visualizationHandler)

    var prefsForShuffle = Statified.myActivity?.getSharedPreferences(Staticated.MY_PREFS_SHUFFLE, Context.MODE_PRIVATE)
    var isShuffleAllowed = prefsForShuffle?.getBoolean("feature", false)
    if (isShuffleAllowed as Boolean) {
        Statified.currentSongHelper?.isShuffle = true
        Statified.currentSongHelper?.isLoop = false
        Statified.shuffleImageButton?.setBackgroundResource(R.drawable.shuffle_icon)
        Statified.loopImageButton?.setBackgroundResource(R.drawable.loop_white_icon)
    } else {
        Statified.currentSongHelper?.isShuffle = false
        Statified.shuffleImageButton?.setBackgroundResource(R.drawable.shuffle_white_icon)
    }


    var prefsForLoop = Statified.myActivity?.getSharedPreferences(Staticated.MY_PREFS_LOOP, Context.MODE_PRIVATE)
    var isLoopAllowed = prefsForLoop?.getBoolean("feature", false)
    if (isLoopAllowed as Boolean) {
        Statified.currentSongHelper?.isShuffle = false
        Statified.currentSongHelper?.isLoop = true
        Statified.shuffleImageButton?.setBackgroundResource(R.drawable.shuffle_white_icon)
        Statified.loopImageButton?.setBackgroundResource(R.drawable.loop_icon)
    } else {
        Statified.currentSongHelper?.isLoop = false
        Statified.loopImageButton?.setBackgroundResource(R.drawable.loop_white_icon)
    }

    if (Statified.favoriteContent?.checkifIdExists(Statified.currentSongHelper?.songId?.toInt() as Int) as Boolean) {
        Statified.fab?.setImageDrawable(ContextCompat.getDrawable(Statified.myActivity, R.drawable.favorite_on))
    } else {
        Statified.fab?.setImageDrawable(ContextCompat.getDrawable(Statified.myActivity, R.drawable.favorite_off))

    }
    seekbarHandler()
}


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    Statified.mSensorManager = Statified.myActivity?.getSystemService(Context.SENSOR_SERVICE) as SensorManager
    mAcceleration = 0.0f
    mAccelerationCurrent = SensorManager.GRAVITY_EARTH
    mAccelerationLast = SensorManager.GRAVITY_EARTH
    bindShakeListener()


}

override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
    menu?.clear()
    inflater?.inflate(R.menu.song_playing_menu, menu)
    super.onCreateOptionsMenu(menu, inflater)

}

override fun onPrepareOptionsMenu(menu: Menu?) {
    super.onPrepareOptionsMenu(menu)
    val item: MenuItem? = menu?.findItem(R.id.action_redirect)
    item?.isVisible = true
    val item2: MenuItem? = menu?.findItem(R.id.action_sort)
    item2?.isVisible = false


}

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
    when (item?.itemId) {
        R.id.action_redirect -> {
            var pos = 0
            if (Statified.back.equals("Favorite", true)) {
                pos = 0
            }
            if (Statified.back.equals("MainScreen", true)) {
                pos = 1
            }
            if (pos == 1) {
                val mainScreenFragment = MainScreenFragment()
                (context as MainActivity).supportFragmentManager
                        .beginTransaction()
                        .replace(R.id.details_fragment, mainScreenFragment)
                        .commit()
            }

            if (pos == 0) {
                val favoriteFragment = FavoriteFragment()
                (context as MainActivity).supportFragmentManager
                        .beginTransaction()
                        .replace(R.id.details_fragment, favoriteFragment)
                        .commit()
            }
            return false
        }

    }
    return false
}


fun clickHandler() {

    Statified.fab?.setOnClickListener({
        if (Statified.favoriteContent?.checkifIdExists(Statified.currentSongHelper?.songId?.toInt() as Int) as Boolean) {
            Statified.fab?.setImageDrawable(ContextCompat.getDrawable(Statified.myActivity, R.drawable.favorite_off))
            Statified.favoriteContent?.deleteFavourite(Statified.currentSongHelper?.songId?.toInt() as Int)
            Toast.makeText(Statified.myActivity, "Removed from favorites", Toast.LENGTH_SHORT).show()
        } else {
            Statified.fab?.setImageDrawable(ContextCompat.getDrawable(Statified.myActivity, R.drawable.favorite_on))
            Statified.favoriteContent?.storeAsFavorite(Statified.currentSongHelper?.songId?.toInt(), Statified.currentSongHelper?.songArtist,
                    Statified.currentSongHelper?.songTitle, Statified.currentSongHelper?.songPath)
            Toast.makeText(Statified.myActivity, "Added to Favorites", Toast.LENGTH_SHORT).show()


        }
    })

    Statified.shuffleImageButton?.setOnClickListener({

        var editorShuffle = Statified.myActivity?.getSharedPreferences(Staticated.MY_PREFS_SHUFFLE, Context.MODE_PRIVATE)?.edit()
        var editorLoop = Statified.myActivity?.getSharedPreferences(Staticated.MY_PREFS_LOOP, Context.MODE_PRIVATE)?.edit()
        if (Statified.currentSongHelper?.isShuffle as Boolean) {
            Statified.currentSongHelper?.isShuffle = false
            Statified.shuffleImageButton?.setBackgroundResource(R.drawable.shuffle_white_icon)
            editorShuffle?.putBoolean("feature", false)
            editorShuffle?.apply()
        } else {
            Statified.currentSongHelper?.isLoop = false
            Statified.currentSongHelper?.isShuffle = true
            Statified.loopImageButton?.setBackgroundResource(R.drawable.loop_white_icon)
            Statified.shuffleImageButton?.setBackgroundResource(R.drawable.shuffle_icon)
            editorShuffle?.putBoolean("feature", true)
            editorShuffle?.apply()
            editorLoop?.putBoolean("feature", false)
            editorLoop?.apply()
        }
    })

    Statified.nextImageButton?.setOnClickListener({
        Statified.currentSongHelper?.isPlaying = true
        if (Statified.currentSongHelper?.isLoop as Boolean) {
            Statified.loopImageButton?.setBackgroundResource(R.drawable.loop_white_icon)
        }
        if (Statified.currentSongHelper?.isShuffle as Boolean) {
            Staticated.playNext("PlayNextLikeNormalShuffle")
        } else {
            Staticated.playNext("PlayNextNormal")
        }
    })

    Statified.previousImageButton?.setOnClickListener({
        Statified.currentSongHelper?.isPlaying = true
        if (Statified.currentSongHelper?.isLoop as Boolean) {
            Statified.loopImageButton?.setBackgroundResource(R.drawable.loop_white_icon)
        }
        Staticated.playPrevious()
    })

    Statified.loopImageButton?.setOnClickListener({

        var editorShuffle = Statified.myActivity?.getSharedPreferences(Staticated.MY_PREFS_SHUFFLE, Context.MODE_PRIVATE)?.edit()
        var editorLoop = Statified.myActivity?.getSharedPreferences(Staticated.MY_PREFS_LOOP, Context.MODE_PRIVATE)?.edit()

        if (Statified.currentSongHelper?.isLoop as Boolean) {
            Statified.currentSongHelper?.isLoop = false
            Statified.loopImageButton?.setBackgroundResource(R.drawable.loop_white_icon)
            editorLoop?.putBoolean("feature", false)
            editorLoop?.apply()
        } else {
            Statified.currentSongHelper?.isLoop = true
            Statified.currentSongHelper?.isShuffle = false
            Statified.loopImageButton?.setBackgroundResource(R.drawable.loop_icon)
            Statified.shuffleImageButton?.setBackgroundResource(R.drawable.shuffle_white_icon)
            editorLoop?.putBoolean("feature", true)
            editorLoop?.apply()
            editorShuffle?.putBoolean("feature", false)
            editorShuffle?.apply()
        }

    })

    Statified.playPauseImageButton?.setOnClickListener({

        if (Statified.mediaPlayer?.isPlaying as Boolean) {
            Statified.mediaPlayer?.pause()
            Statified.currentSongHelper?.isPlaying = false
            Statified.playPauseImageButton?.setBackgroundResource(R.drawable.play_icon)
        } else {
            Statified.mediaPlayer?.start()
            Statified.currentSongHelper?.isPlaying = true
            Statified.playPauseImageButton?.setBackgroundResource(R.drawable.pause_icon)
            Staticated.processInformation(Statified.mediaPlayer as MediaPlayer)
        }

    })


}


fun bindShakeListener() {

    Statified.mSensorListener = object : SensorEventListener {
        override fun onAccuracyChanged(p0: Sensor?, p1: Int) {

        }

        override fun onSensorChanged(p0: SensorEvent) {

            val x = p0.values[0]
            val y = p0.values[1]
            val z = p0.values[2]

            mAccelerationLast = mAccelerationCurrent
            mAccelerationCurrent = Math.sqrt(((x * x + y * y + z * z).toDouble())).toFloat()
            val delta = mAccelerationCurrent - mAccelerationLast
            mAcceleration = mAcceleration * 0.9f + delta
            if (mAcceleration > 12) {
                println("11111")
                val prefs = Statified.myActivity?.getSharedPreferences(Statified.MY_PREFS_NAME, Context.MODE_PRIVATE)
                val isAllowed = prefs?.getBoolean("feature", false)
                if (isAllowed as Boolean && Statified.check == true) {
                    Statified.currentSongHelper?.isPlaying = true
                    if (Statified.currentSongHelper?.isLoop as Boolean) {
                        Statified.loopImageButton?.setBackgroundResource(R.drawable.loop_white_icon)
                    }
                    if (Statified.currentSongHelper?.isShuffle as Boolean) {
                        Staticated.playNext("PlayNextLikeNormalShuffle")
                    } else {
                        Staticated.playNext("PlayNextNormal")
                    }
                    Statified.check = false
                }
            }
        }

    }
}

fun seekbarHandler() {
    val seekbarListener = SeekBarController()
    Statified.seekbar?.setOnSeekBarChangeListener(seekbarListener)
}

2 个答案:

答案 0 :(得分:1)

让我们尝试一下,

这是Java代码,只要您可以将其转换为kotlin,就可以正常工作。

在您的活动中

声明静态变量

public static boolean IS_MUSIC_SCREEN = false;

实施此方法

      @Override
    public void onBackPressed() {
        if (IS_MUSIC_SCREEN) {
            IS_MUSIC_SCREEN=false;
            startActivity(new Activity(this,MainActivity.class));
        }else{
            super.onBackPressed();
        }
    }

在您的片段中,

在onCreateView中设置的IS_MUSIC_SCREEN为真

喜欢这个,

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_song_playing, container, false);
        MainActivity.IS_MUSIC_SCREEN =true;
        return view;
    }

答案 1 :(得分:0)

一种简单的方法是使用BackHandler初始化Fragment机制。我的BaseFragment类中有这样一种方法,如果后向动作是由片段而不是活动来处理的,则它将布尔返回true。为此,您可以使用自定义LifecycleAdapter

interface LifecycleAdapter {
   fun onBackPressed() : Boolean = false

   // add other methods like onActivityResult or similar 
   // if needed that is to be delegated to the fragment
}

现在在BaseActivity中,您将拥有2个函数,一个用于添加Fragment,一个寄存器函数,用于MutableList<WeakReference<LifecycleAdapter>>

open class BaseActivity : AppCompatActivity() { // which ever type
    protected open val lifecycleReferrals : MutableList<WeakReference<LifecycleAdapter>> = mutableListOf()

    fun addFragment(fragment: BaseFragment, 
            @IdRes containerId: Int = R.id.fragment_container, 
            replace: Boolean = true, 
            addToBackstack: Boolean = false) {

        supportFragmentManager?.beginTransaction()?.run {
            if (addToBackstack){
                addToBackStack(fragment.javaClass.simpleName)          
            }
            if (replace) {
                replace(containerId, fragment) // add tag when needed
            } else {
                add(containerId, fragment)
            }
            registerLifecycleReferrals(fragment)
            commit()

        }
    }

    fun registerLifecycleReferrals(adapter: LifecycleAdapter){
        lifecycleReferrals.add(WeakReference(adapter))
    }


    override fun onBackPressed() {
        if (lifecycleReferrals.none { it.get()?.onBackPressed(0) == true }) {
            super.onBackPressed()
        }
    }

}

如果您需要访问onBackPressed()之类的控制器方法的任何类,则可以在所需的类中实现接口并从registerLifecycleReferrals(obj)调用activity

现在您的基本片段类将是:

open class BaseFragment : Fragment(), LifecycleAdapter{
    // have parent level constructs and implementation
    // that would serve you well across the entire project
}

您当前的片段可以是:

class SongPlayingFragment : BaseFragment() {
    // do all fragment inits and override methods

    override fun onBackPressed(): Boolean {
        if (Statified.currentSongHelper?.isPlaying == true) {
             // handle the call and switch to MainScreenActivity
             context?.apply {
                 startActivity<MainScreenActivity>() // anko extension
                 return true
             }
        }
        return super.onBackPressed()
    }
}

注意:在BaseActivity类中对适配器的引用很弱,因为如果Fragment进行销毁过程,则GC可以轻松释放适配器,并且列表不会保留对其的引用。