如果已从启动器图标暂停并重新启动,则应用程序将返回到第一活动

时间:2018-09-04 14:21:08

标签: android android-recyclerview kotlin

基本上,每当我启动第二项活动并通过点击启动器中的图标暂停应用程序并重新启动应用程序时,应用程序不会在第二项活动中恢复,而是会返回到第一项活动。最重要的是,如果我暂停应用程序(任务管理器中的视图),那么哪个应用程序会再次显示整个活动,就像真的快(以毫秒为单位),然后继续执行任务管理器。

清单文件

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:replace="android:icon">
    <activity
        android:name=".Activity.MainActivity"
        android:launchMode="singleTask">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Activity.ReferenceActivity"
        android:configChanges="orientation|screenSize" /> <!-- Find a better solution for orientation change -->

    <meta-data
        android:name="preloaded_fonts"
        android:resource="@array/preloaded_fonts" />

    <activity android:name=".Activity.AboutActivity"></activity>
</application>

由于您会注意到launchMode设置为“ singleTask”,因此背后有一个原因。我的应用程序包含回收者视图。其数据是从第二个活动更新的。如果没有将launchMode设置为singleTask,则不会更新recyclerview数据,因此要更新数据,我必须重新启动整个应用程序才能看到任何数据更改。这是一个临时的解决方法,但我希望有人也能在此问题上为我提供帮助。

关于雷克莱尔的观点没有更新,我无数次询问了该问题,但从未得到解决方案。是的,我可能在堆栈上遇到了数百个类似的问题,但是这些问题都不起作用。请记住,我是Android开发的初学者,因此我对MvvM,RxJava或实时数据架构等高级知识不了解。

P.S就是这样,任何人都提供了一种解决方案,我可以在onStart或Resume的某个地方再次调用我的数据加载器功能,我已经尝试了无数次,但是它不起作用。我的应用程序不使用片段或任何其他高级内容,而只是基本活动。任何帮助表示赞赏!

第二活动

class ReferenceActivity : AppCompatActivity() {

private var dbHandler: PediaDatabase? = null
private var note = UserNotes()
private var noteExisted: Boolean = false
private var cardAdapterPos: Int? = null
private var title: String? = null
private var text: String? = null
private var sharingMenuOpened: Boolean = false

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_reference)


    val toolbarRef: Toolbar = findViewById(R.id.toolbarRefID)
    setSupportActionBar(toolbarRef)

    val toolbarTxtView = findViewById<TextView>(R.id.refToolbarTitleID)
    supportActionBar!!.setDisplayShowTitleEnabled(false)

    overridePendingTransition(R.anim.slide_in, R.anim.slide_out)




    dbHandler = PediaDatabase(this)
    val data = intent
    if (!isNewNote) {
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)

        if (data != null) {
            noteExisted = true

            this.cardAdapterPos = data.extras.getInt("cardPosition")
            cardID = data.extras.getInt("cardID")


            existingNote = dbHandler!!.readNote(cardID)

            text = existingNote.noteText
            title = existingNote.noteTitle

            refTitleID.setText(existingNote.noteTitle, TextView.BufferType.EDITABLE)
            refTextID.setText(existingNote.noteText, TextView.BufferType.EDITABLE)
            toolbarTxtView.text = "Created: " + existingNote.noteDate.toString()



        }
    } else {
        toolbarTxtView.text = "New note"

        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
    }
}

override fun onResume() {
    super.onResume()
    sharingMenuOpened = false
}


override fun onPause() {
    super.onPause()

    if (!sharingMenuOpened)
        saveNote()
}


override fun onCreateOptionsMenu(menu: Menu?): Boolean {

    menuInflater.inflate(R.menu.top_menu, menu)
    val addItem: MenuItem = menu!!.findItem(R.id.add_note_menu)
    val delItem: MenuItem = menu.findItem(R.id.delete_note_menu)
    val shareButton: MenuItem = menu.findItem(R.id.shareID)


    addItem.isVisible = false
    delItem.isVisible = false
    shareButton.isVisible = false

    if (noteExisted) {
        delItem.isVisible = true
        shareButton.isVisible = true
    }

    return true
}

override fun onOptionsItemSelected(item: MenuItem?): Boolean {

    if (item!!.itemId == R.id.delete_note_menu) {

        val deletionMsg = SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE)
        deletionMsg.titleText = "Delete this note?"
        deletionMsg.confirmText = "Yes"
        deletionMsg.setCancelable(false)



        deletionMsg.setConfirmClickListener(object : SweetAlertDialog.OnSweetClickListener {
            override fun onClick(sweetAlertDialog: SweetAlertDialog?) {

                dbHandler!!.deleteNote(cardID)


                deletionMsg.dismissWithAnimation()

                val successMsg = SweetAlertDialog(this@ReferenceActivity, SweetAlertDialog.SUCCESS_TYPE)
                successMsg.setCancelable(false)
                successMsg.titleText = "Note deleted!"

                successMsg.setConfirmClickListener(object : SweetAlertDialog.OnSweetClickListener {
                    override fun onClick(sweetAlertDialog: SweetAlertDialog?) {

                        successMsg.dismissWithAnimation()
                        finish()
                    }

                }).show()
            }
        })

        deletionMsg.setCancelButton("No", object : SweetAlertDialog.OnSweetClickListener {
            override fun onClick(sweetAlertDialog: SweetAlertDialog?) {
                deletionMsg.dismissWithAnimation()
            }
        })
        deletionMsg.show()



    }

    if (item.itemId == R.id.shareID) {

        var sharingTitle: String = title!!.trim()
        var sharingText: String = text!!.trim()

        sharingMenuOpened = true

        val sharingIntent = Intent(android.content.Intent.ACTION_SEND)
        sharingIntent.type = "text/plain"
        val shareBody: String? = sharingText
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, sharingTitle)
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody)
        startActivity(Intent.createChooser(sharingIntent, "Share to"))
    }

    return super.onOptionsItemSelected(item)
}

private fun saveNote() {
    title = refTitleID.text.toString().trim()
    text = refTextID.text.toString().trim()


    if (existingNote.noteText == text && existingNote.noteTitle == title) {
        finish()
    } else {

        if (noteExisted) {

            if (TextUtils.isEmpty(title) && TextUtils.isEmpty(text)) {
                dbHandler!!.deleteNote(cardID)

                val parsedColor = Color.parseColor("#263238")
                Toasty.Config.getInstance().setInfoColor(parsedColor).apply()
                Toasty.info(this, "Note deleted", Toast.LENGTH_SHORT, true).show()
            } else {

                if (TextUtils.isEmpty(title))
                    title = "No title"

                existingNote.noteTitle = title
                existingNote.noteText = text
                existingNote.noteDate = System.currentTimeMillis().toString()

                dbHandler!!.updateNote(existingNote)
                Toasty.success(this, "Note saved", Toast.LENGTH_SHORT, true).show()


                startActivity(Intent(this, MainActivity::class.java))
                finish()
            }

        } else {
            if (TextUtils.isEmpty(title) && TextUtils.isEmpty(text)) {
                finish()
            } else {
                if (TextUtils.isEmpty(title))
                    title = "No title"

                note.noteTitle = title
                note.noteText = text
                note.noteDate = System.currentTimeMillis().toString()

                dbHandler!!.createNote(note)
                Toasty.success(this, "Note saved", Toast.LENGTH_SHORT, true).show()


                startActivity(Intent(this, MainActivity::class.java))
                finish()
            }
        }
    }
}

}

2 个答案:

答案 0 :(得分:0)

您可以尝试在MainActivty中使用它:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    if (Intents.isActivityExpandedFromLauncherIcon(this)) {
        finish()
    } else {
        setContentView(R.layout.activity_main)
}
}

更新:

public class Intents {

public static boolean isActivityExpandedFromLauncherIcon(@NonNull Activity activity) {
    return !activity.isTaskRoot() && isActivityStartedFromLauncherIcon(activity.getIntent());
}

public static boolean isActivityStartedFromLauncherIcon(@Nullable Intent intent) {
    return intent != null &&
            intent.hasCategory(Intent.CATEGORY_LAUNCHER) &&
            intent.getAction() != null &&
            intent.getAction().equals(Intent.ACTION_MAIN);
}

}

答案 1 :(得分:0)

您正在通过finish()调用的saveNote()方法调用onPause(),因此,每次您的应用进入后台时,由于调用,当前活动都将结束(关闭)其中的finish()

finish()方法删除对saveNote()的调用,您将恢复到当前活动,而不是登陆启动器/主活动。