简体   繁体   中英

Serialized Intent Extras seem empty after starting new Activity

So i've benn working on App which needs a BlockingQueue to communicate between the UI Thread and a handler thread. So i createt a LinkedBlockingQueue and the Thread in the onCreate of my first Activity.

BlockingQueue<String> UIqueueRx = new LinkedBlockingDeque<String>();
BlockingQueue<String> UIqueueTx = new LinkedBlockingDeque<String>();

final main m = new main(UIqueueRx,UIqueueTx);
Thread myMainThread = new Thread(m);
myMainThread.setName("AdapterThread");
myMainThread.start();

I need the BlockingQueue also during the next Actyvities, so put them into the Intent.putExtra and startet next Activity.

Intent myIntent = new Intent(v.getContext(),Start.class);
myIntent.putExtra("UIqueueRx",(Serializable) UIqueueRx);
myIntent.putExtra("UIqueueTx",(Serializable) UIqueueTx);
startActivity(myIntent);

So in my new Actyvity i wanted to take the BlockingQueue fron the Intent and bind it to my local class.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start);
    BlockingQueue<String> UIqueueRx = (BlockingQueue<String>) getIntent().getSerializableExtra("UIqueueRx");
    BlockingQueue<String> UIqueueTx = (BlockingQueue<String>) getIntent().getSerializableExtra("UIqueueTx");
    //...
}

But if i later use the Queue to send Strings to the Thread nothing hapens, the String stays in Queue. So i tried to debug and mentions the id number of the Queues changed so they arent connected anymore. I also checked the Intent itself, after open the new activity there are no Extras anymore.

Anyone mention a mistake i overlooked? Is this kind of code even rational or is there a spimler way?

The onCreate() method only gets called once when the Activity is created. If its paused, resumed or restarted later on it skips the onCreate Method.

http://developer.android.com/images/activity_lifecycle.png

If you want to persist the Extras from the Intent, you should use class fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM