如何最小化我的应用程序状态?

时间:2016-01-23 15:56:35

标签: java android bundle state minimize

我正在使用Android Studio / java。

我的应用程序最小化时需要保存一些自定义变量。我试图制作一个Bundle,可以通过onSaveInstanceState()保存,这似乎有效。问题是在应用程序再次处于活动状态时检索它。根据我的理解,我可以通过onRestoreInstanceState()和Oncreate()获得它。问题是永远不会调用onRestoreInstanceState(),并且在从最小化状态重新打开后不会调用OnCreate()。

我希望能够按下主页按钮(当前似乎正在正确保存包),然后通过“最近”按钮重新打开应用程序。当我这样做时,应用程序重置,我无法得到我刚刚保存的Bundle。我该怎么做?

编辑:调用finish()不起作用。我的活动是在另一项活动之上,所以如果我这样做,我就会结束之前的活动。

3 个答案:

答案 0 :(得分:0)

亲爱的朋友,你必须使用共享偏好。

(对于您提出的有问题的小任务)

共享偏好设置

设定值------------->

Option Explicit
Global sldmissed As Slide
Global c As Long
Sub Highlightkeywords()
 Dim Pres As Presentation
 Dim shp As Shape
 c = 0
 For Each Pres In Application.Presentations
      For Each sldmissed In Pres.Slides
         For Each shp In sldmissed.Shapes
            If keywords(shp) Then
            Exit Sub
         Next shp
     Next sldmissed
 Next Pres

End Sub

Function keywords(shp As Object) As Boolean

    Dim txtRng As TextRange
    Dim rngFound As TextRange
    Dim I, K, X, n As Long
    Dim iRows As Integer
    Dim iCols As Integer
    Dim TargetList

    keywords = True


    TargetList = Array("1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "29th", "30th", "31st", "etc", ":00", ".00", "a.m.", "p.m.", "number", "US", "USA", "$")

    With shp

    If shp.HasTable Then

                For iRows = 1 To shp.Table.Rows.Count
                    For iCols = 1 To shp.Table.Rows(iRows).Cells.Count
                        Set txtRng = shp.Table.Rows(iRows).Cells(iCols).Shape.TextFrame.TextRange
                                 For I = LBound(TargetList) To UBound(TargetList)
                                    Set rngFound = txtRng.Find(FindWhat:=TargetList(I), MatchCase:=True, wholewords:=True)
                                    Do While Not rngFound Is Nothing
                                    n = rngFound.Start + 1
                                    With rngFound
                                        If rngFound.Font.Color.RGB = RGB(255, 0, 0) Then
                                            sldmissed.Select
                                            c = c + 1
                                            MsgBox "Slide: " & sldmissed.SlideNumber, vbInformation
                                            Set rngFound = txtRng.Find(TargetList(I), n, MatchCase:=True, wholewords:=True)

                                            keywords = False
                                            GoTo Normalexit


                                        Else
                                             keywords = False
                                            GoTo Normalexit



                                    End If
                             End With
                            Loop
                        Next
                    Next
                Next

            End If

            End With


           Select Case shp.Type
            Case msoTable


            Case msoGroup
                For X = 1 To shp.GroupItems.Count
                    Call keywords(shp.GroupItems(X))
                Next X

            Case 21
                For X = 1 To shp.Diagram.Nodes.Count
                    Call keywords(shp.GroupItems(X))
                Next X

            Case Else

                If shp.HasTextFrame Then
                Set txtRng = shp.TextFrame.TextRange
                    For I = LBound(TargetList) To UBound(TargetList)
                       Set rngFound = txtRng.Find(FindWhat:=TargetList(I), MatchCase:=True, wholewords:=True)
                       Do While Not rngFound Is Nothing
                             n = rngFound.Start + 1
                             With rngFound
                               If rngFound.Font.Color.RGB = RGB(255, 0, 0) Then
                                        sldmissed.Select
                                        c = c + 1
                                        MsgBox "Slide: " & sldmissed.SlideNumber, vbInformation
                                        Set rngFound = txtRng.Find(TargetList(I), n, MatchCase:=True, wholewords:=True)
                                        keywords = False
                                        GoTo Normalexit


                                Else
                                        keywords = False
                                        GoTo Normalexit




                                    End If
                             End With
                        Loop
                    Next
                  End If

    End Select

Normalexit:
End Function

获取价值------------->

SharedPreferences pref =   
PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = pref.edit();
edit.putString("username", "billy");
edit.putString("user_id", "65");
edit.commit(); 

还有其他人(如果你想保留你的数据)

  
    
      
        
          

Android框架提供了多种持久性选项和策略:

        
      
    
  

共享首选项 - 轻松将基本数据保存为私有持久词典中的键值对。

本地文件 - 将任意文件保存到内部或外部设备存储。

SQLite数据库 - 在应用程序特定数据库中的表中保留数据。

ORM - 使用更高级别的查询/更新语法描述和保留模型对象。

  
    
      
        
          

每个存储选项都有典型的相关用例,如下所示:

        
      
    
  

共享偏好设置 - 用于应用偏好设置,密钥和会话信息。

本地文件 - 通常用于blob数据或数据文件缓存(即磁盘映像缓存)

SQLite数据库 - 用于复杂的本地数据操作或原始速度

ORM - 用于在本地存储简单的关系数据以减少SQL样板

答案 1 :(得分:0)

如果您只需要保存状态(而不是将其保存到本地存储),

onSave / RestoreInstanceState是正确的方法。

您必须覆盖protected void onSaveInstanceState(Bundle bundle)并使用参数中的Bundle添加更多数据。另外,不要忘记致电super.onSaveInstanceState(bundle)。覆盖protected方法非常重要,而不是public方法。同样适用于protected void onRestoreInstanceState(Bundle bundle)。您还可以在onCreate()中恢复状态,但是您需要在那里检查null

有关如何准确实现正确行为的教程,请参阅official Android training docs

答案 2 :(得分:0)

除非你的应用程序是从内存中卸载的,否则不会调用onRestoreInstanceState()(当你立即返回时不会发生这种情况)。尝试最小化它,然后打开一些其他应用程序,然后尝试重新打开你的应用程序 - 应该调用onRestoreInstanceState()。

请注意,当您更改设备方向时,也会始终调用onRestoreInstanceState()。(

相关问题