活动的生命周期表现不同软糖起 [英] Activity Lifecycle behaves differently JellyBean onwards

查看:130
本文介绍了活动的生命周期表现不同软糖起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情形。活动A开始活动B. B开始上另一个活动C的开始通知点击。现在的行为是对姜饼和ICS起不同。柜面姜饼,当我点击的通知,预期的行为是看到的,但是当我运行ICS或豆形软糖一样code,当我点击通知活动A被破坏(的OnDestroy被调用)。为什么是生命周期行为不同。我怎样才能使它的行为在所有的设备以一致的方式?请建议。

 公共类MainActivity延伸活动{

        @覆盖
        保护无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.activity_main);
            Log.v(MyLog,活动上创建了);
        }

        @覆盖
        保护无效的onDestroy(){
            super.onDestroy();
            Log.v(MyLog,活动已销毁);

        }
        公共无效startB(视图v)
        {
            意向意图=新的意图(getApplicationContext(),B.class);
            startActivity(意向);
        }

        保护无效的onStop()
        {
            super.onStop();
            Log.v(MyLog,活动已停止);
        }

        保护无效onResume()
        {
            super.onResume();
            Log.v(MyLog,活动继续执行的);
        }


    }
}

大众B级延伸活动{
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.b);
        Log.v(MyLog,b活动创造了);
    }

    保护无效的onStop()
    {
        super.onStop();
        Log.v(MyLog,b活动已停止);
    }

    保护无效onResume()
    {
        super.onResume();
        Log.v(MyLog,b活动恢复);
    }

    公共无效startNotification(视图v)
    {
        NotificationCompat.Builder mBuilder =
                新NotificationCompat.Builder(getApplicationContext())
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle(我的通知)
        .setContentText(的Hello World!)
        .setAutoCancel(真正的);



        //创建一个明确的意图在你的应用程序的活动
        意图resultIntent =新的意图(getApplicationContext(),C.class);

        //堆栈生成器对象将包含人工回堆栈的
        //开始活动。
        //这样可以确保从活动向后导航引领出
        //你的应用程序至主屏​​幕。
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
        //添加背部栈的意图(而不是意图本身)
        stackBuilder.addParentStack(C.class);
        //添加的活动开始到堆栈的顶部的意图
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        // MID可以让你以后更新通知。
        INT MID = 1;
        mNotificationManager.notify(MID,mBuilder.build());
    }
}

公共C类延伸活动{
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.c);
        Log.v(MyLog,活动的C创建);
    }
    保护无效的onStop()
    {
        super.onStop();
        Log.v(MyLog,活动Ç停止);
    }

    保护无效onResume()
    {
        super.onResume();
        Log.v(MyLog,活动Ç恢复);
    }
}
 

解决方案

系统在需要时可以随时终止活动。也许它不是exectly不同的设备/不同的Andr​​oid版本乳宁的可用内存量之间的Andr​​oid版本之间的差异,但是型差分。

I have the following scenario. Activity A starts Activity B. B starts a Notification clicking on which another activity C is started. Now the behaviour is different on Gingerbread and ICS onwards. Incase of Gingerbread when I click on the Notification, the expected behavior is seen, however when I run the same code on ICS or JellyBean when I click on the Notification Activity A is destroyed(OnDestroy is called). Why is the lifecycle behavior different. How can I make it to behave in a consistent way in all devices? Please suggest.

    public class MainActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Log.v("MyLog","Activity A created");
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            Log.v("MyLog","Activity A destroyed");

        }
        public void startB(View v)
        {
            Intent intent=new Intent(getApplicationContext(),B.class);
            startActivity(intent);
        }

        protected void onStop()
        {
            super.onStop();
            Log.v("MyLog","Activity A stopped");
        }

        protected void onResume()
        {
            super.onResume();
            Log.v("MyLog","Activity A resumed");
        }


    }
}

public class B extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.b);
        Log.v("MyLog","Activity B created");
    }

    protected void onStop()
    {
        super.onStop();
        Log.v("MyLog","Activity B stopped");
    }

    protected void onResume()
    {
        super.onResume();
        Log.v("MyLog","Activity B resumed");
    }

    public void startNotification(View v)
    {
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(getApplicationContext())
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        .setAutoCancel(true);



        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(getApplicationContext(), C.class);

        // The stack builder object will contain an artificial back stack for the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(C.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // mId allows you to update the notification later on.
        int mId=1;
        mNotificationManager.notify(mId, mBuilder.build());
    }
}

public class C extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.c);
        Log.v("MyLog","Activity C created");
    }
    protected void onStop()
    {
        super.onStop();
        Log.v("MyLog","Activity C stopped");
    }

    protected void onResume()
    {
        super.onResume();
        Log.v("MyLog","Activity C resumed");
    }
}

解决方案

System can terminate activity always when it's needed. Probably it's not exectly difference between Android releases, but differece between different devices / free memory amount of different Android versions runing.

这篇关于活动的生命周期表现不同软糖起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆