Android Activity(应用程序)生命周期

时间:2015-11-29 08:32:14

标签: android android-lifecycle

所以我目前正在android studio上创建一个应用程序,并且得到了这个错误,但我并不是真正理解这个问题。所以目前我正在实现一个sharedPreference来存储我的用户登录会话,这样用户就不必继续登录了。但问题是,当我退出主启动器(主要活动)并返回应用程序时,它会转到主要活动,但没有任何按钮工作。

我环顾四周,发现也许我需要实现更好的app lifecyle流程(onResume,onPause ...)。现在我已经创建了其他应用程序,如天气应用程序,但没有一个具有这些方法的实现。所以我想知道是否有人遇到过这个问题可以帮助我找到解决问题的具体方法。

我可能完全错误的解决方案是什么,但这里是我的主要活动onCreate方法的代码。也许那里出了点问题

  @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_profile);
      ActionBar bar=getSupportActionBar();
      bar.hide();

    // get notifications from the updated months
    DateFormat dateFormat = new SimpleDateFormat("MMMM");
    Date date = new Date();
    //Log.d("Month",dateFormat.format(date));
    Calendar mCalendar = Calendar.getInstance();
    int currentMonth = mCalendar.get(Calendar.MONTH);
    String curMonth = months[currentMonth];
    // sends the notification for when new apples are in season
    if(dateFormat.format(date) == curMonth){
        notificationSetter();
    }
    // initializing user's session
    session = new UserSession(getApplicationContext());

    //this will check if the user is logged in. if not: redirected to MainLogin

    session.checkLogin();
    HashMap<String,String> user = session.getUserDetails();

    if(user.get(UserSession.KEY_NAME) == null)
    {
        Intent intent = new Intent(this, MainLogin.class);
        // these flags are added to make the login activity be the front of our app. so when you
        // press the back button, it goes to the home page of the phone
        // saying that login activity should be new task and the old task should be cleared
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }


    Bundle appleData = getIntent().getExtras();
    if (appleData == null)
        return;
    currentTable = appleData.getString("username");
    Log.e("current", currentTable);



    // search button
    mSearch = (Button)findViewById(R.id.searchButton);
    mSearch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Profile.this, SearchActivity.class);
            intent.putExtra("username", currentTable);
            startActivity(intent);
        }
    });

    // inSeason button
    inSeason = (Button)findViewById(R.id.seasonButton);
    inSeason.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Profile.this, Apple_Season_Activity.class);
            intent.putExtra("username", currentTable);
            startActivity(intent);
        }
    });

    // basket button
    basketButton = (Button)findViewById(R.id.basketButton);
    basketButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Profile.this, FavoriteActivity.class);
            intent.putExtra("username", currentTable);
            startActivity(intent);
        }
    });
}

0 个答案:

没有答案