2014-05-21 46 views
1

有关Android应用程序初始化的快速问题。 很多时候,初始化任务放置在onCreate/onResume方法的主要活动中。何处放置Android应用程序初始化代码?

现在的问题是,我不能保证主要活动代码总是运行。

例如: 我有MainActivity与使用意图引发AnotherActivity按钮:

this.startActivity(new Intent(this, AnotherActivity.class)

现在想象用户敲击按钮,当用户导航前进的应用程序放入背景到另一个应用程序或东西。

因为OS资源政策

一段时间后,应用过程中可能会被杀死。

当我回去的应用程序,该进程重新启动,但只有AnotherActivity被实例化“运行”。所以初始化代码被跳过。

我认为Application.onCreate可能是一个不错的选择,但它不应该包含对多耗时代码。

我交给了一些项目,该项目的主要活动是SplashActivity,即初始化一些核心应用组件,有时候,这个过程重新启动后,由于这种行为的应用程序中断。

这是发生了什么:

05-21 23:14:30.992 D/TESTAPP (22340): onCreate 
05-21 23:14:31.002 D/TESTMAIN(22340): onCreate(null) 
05-21 23:14:31.032 D/TESTMAIN(22340): onStart 
05-21 23:14:31.032 D/TESTMAIN(22340): onResume 
05-21 23:14:36.922 D/TESTMAIN(22340): onPause 
05-21 23:14:36.932 D/TESTOTHER(22340): onCreate(null) 
05-21 23:14:36.932 D/TESTOTHER(22340): Message: Hello World 
05-21 23:14:36.952 D/TESTOTHER(22340): onStart 
05-21 23:14:36.952 D/TESTOTHER(22340): onResume 
05-21 23:14:37.342 D/TESTMAIN(22340): onSaveInstanceState 
05-21 23:14:58.312 D/TESTOTHER(22340): onPause 
05-21 23:14:58.812 D/TESTOTHER(22340): onSaveInstanceState 
05-21 23:19:47.342 D/TESTAPP (24928): onCreate 
05-21 23:19:47.342 D/TESTOTHER(24928): onCreate(not null) 
05-21 23:19:47.342 D/TESTOTHER(24928): Message: null 
05-21 23:19:47.392 D/TESTOTHER(24928): onStart 
05-21 23:19:47.392 D/TESTOTHER(24928): onRestoreInstanceState 
05-21 23:19:47.392 D/TESTOTHER(24928): onResume 

TESTAPP是应用程序日志标签。 TESTMAIN是主要活动。 TESTOTHER再从主活动按钮

启动的应用程序被投入到后台后(见二十三时14分58秒)我已经通过运行一些其他应用程序和几分钟后处理放一些内存压力的OS上遇难(我不确定是否因内存问题或超时而死亡)。

无论如何,我使用adb shell和ps命令来发现进程何时消失并导航回应用程序(请参阅23:19:47)。没有痕迹的TESTMAIN。

主要活动简单地设置一些静态字符串的Hello World。 该字符串被其他活动使用。当进程重新启动时,Hello World消失了。

代码:

public class StaticContainer { 
    public static String Message; 
} 

public class MyApplication extends Application { 
    private static final String TAG = "TESTAPP"; 

    @Override 
    public void onCreate() { 
     Log.d(TAG, "onCreate"); 
     super.onCreate(); 
    } 
} 

public class MainActivity extends Activity { 
    private static final String TAG = "TESTMAIN"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     Log.d(TAG, String.format("onCreate(%s)", savedInstanceState == null ? "null" : "not null")); 
     StaticContainer.Message = "Hello World"; 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     if (savedInstanceState == null) { 
      getFragmentManager().beginTransaction() 
       .add(R.id.container, new PlaceholderFragment()).commit(); 
     } 
    } 

    @Override 
    public void onResume() { 
     Log.d(TAG, "onResume"); 

     super.onResume(); 
    } 

    @Override 
    public void onPause() { 
     Log.d(TAG, "onPause"); 

     super.onPause(); 
    } 

    @Override 
    public void onStart() { 
     Log.d(TAG, "onStart"); 

     super.onStart(); 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     Log.d(TAG, "onSaveInstanceState"); 

     super.onSaveInstanceState(outState); 
    } 

    @Override 
    protected void onRestoreInstanceState(Bundle savedInstanceState) { 
     Log.d(TAG, "onRestoreInstanceState"); 

     super.onRestoreInstanceState(savedInstanceState); 
    } 

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

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 
    public static class PlaceholderFragment extends Fragment { 
     public PlaceholderFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
      Button other = (Button)rootView.findViewById(R.id.other); 
      other.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Intent intent = new Intent(getActivity(), OtherActivity.class); 
        getActivity().startActivity(intent); 
       } 
      }); 

      return rootView; 
     } 
    } 
} 

public class OtherActivity extends Activity { 
    private static final String TAG = "TESTOTHER"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     Log.d(TAG, String.format("onCreate(%s)", savedInstanceState == null ? "null" : "not null")); 
     Log.d(TAG, "Message: " + StaticContainer.Message); 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_other); 

     if (savedInstanceState == null) { 
      getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit(); 
     } 
    } 
    ...the rest is the same as for the MainActivity (the only item missing is the button and its handler. 

谢谢。

+0

你能发布你所指的代码? – BlackHatSamurai

+0

我刚刚做到了。谢谢。 – aguyngueran

+0

代码或多或少是由Eclipse模板项目+附加日志记录+启动另一个活动的按钮给出的。 – aguyngueran

回答

0

使用SharedPreferences来存储您要设置供其他活动使用的主要数据类型。这些值可以被应用程序中的任何其他活动检索到,并且无论任何活动遭到破坏,都可以保持可用。

例如,在被称为共享偏好设置字符串值“app_values”:

SharedPreferences sharedPreferences = getSharedPreferences("app_values", MODE_PRIVATE); 
sharedPreferences.edit().putString("my_key_string", "Hello World").commit(); 

为了取回被称为共享偏好该字符串值“app_values”:

SharedPreferences sharedPreferences = getSharedPreferences("app_values", MODE_PRIVATE); 
String myString = sharedPreferences.getString("my_key_string", "Default value if key not found"); 
+0

是的,在某些情况下使用SharedPreferences可能是一个解决方案。但我已经使用简单的静态字符串属性仅用于演示目的。我的初始化代码更复杂,它涉及通过提供上下文初始化服务,设置一些其他静态成员。它们中的大多数不能放入SharedPreferences中。谢谢您的帮助。 – aguyngueran

相关问题