Pass the package between three actions in Android

I have three actions, which I can name A, B and C. I want to transfer information between three, like this: A → B → C → A. In A I want to check if there is a transmitted packet (for the first time, when start, they will not be, for example). Data is transferred from AB with normal stratification. From B → C, I use this:

Intent i = new Intent(getApplicationContext(), FlashcardView.class); i.putExtra("rattning", rattning); i.putExtra("noqs", noqs); i.putExtra("categoryid", categoryid); CreateTestView.this.finish(); startActivityForResult(i, 0); 

It is accepted and then sent in the following way:

  Intent data = new Intent(FlashcardView.this, MenuView.class); data.putExtra("point", point); data.putExtra("noqs", noqs); setResult(RESULT_OK, data); finish(); 

It is obtained as follows:

  @Override protected void onActivityResult( int req, int resp, Intent data ) { super.onActivityResult(req, resp, data); // process your received "data" from GameActivity ... Bundle b = getIntent().getExtras(); noqs = b.getInt("noqs"); point = b.getInt("point"); mTvCat.setText("hhhhhh"+point+noqs); publishOnFacebook(point,noqs); } 

It seems like the beam gets lost on the way from C → A. When I transferred it back from C → B, there were no problems. I think this happens because B is activity starting with C, and therefore C goes back to B, not A. I made a turn by calling finish () on B, so C goes back to A. BUt, doing this, i'm losing the package. Or at least I think so.

Has anyone seen this before? Is there a better way to do this? How can I prevent packet loss when switching between more than two actions? Thanks!

Edit:

Edit: Here is the error code when receiving the broadcast:

  03-07 12:57:59.394: ERROR/AndroidRuntime(803): java.lang.RuntimeException: Error receiving broadcast Intent { act=my_action (has extras) } in com.crystalcodeab.Flashcard.MenuView$1@47681ad0 03-07 12:57:59.394: ERROR/AndroidRuntime(803): at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:981) 03-07 12:57:59.394: ERROR/AndroidRuntime(803): at android.os.Handler.handleCallback(Handler.java:587) 03-07 12:57:59.394: ERROR/AndroidRuntime(803): at android.os.Handler.dispatchMessage(Handler.java:92) 03-07 12:57:59.394: ERROR/AndroidRuntime(803): at android.os.Looper.loop(Looper.java:143) 03-07 12:57:59.394: ERROR/AndroidRuntime(803): at android.app.ActivityThread.main(ActivityThread.java:5068) 03-07 12:57:59.394: ERROR/AndroidRuntime(803): at java.lang.reflect.Method.invokeNative(Native Method) 03-07 12:57:59.394: ERROR/AndroidRuntime(803): at java.lang.reflect.Method.invoke(Method.java:521) 03-07 12:57:59.394: ERROR/AndroidRuntime(803): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 03-07 12:57:59.394: ERROR/AndroidRuntime(803): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 03-07 12:57:59.394: ERROR/AndroidRuntime(803): at dalvik.system.NativeStart.main(Native Method) 03-07 12:57:59.394: ERROR/AndroidRuntime(803): Caused by: java.lang.NullPointerException 03-07 12:57:59.394: ERROR/AndroidRuntime(803): at com.crystalcodeab.Flashcard.MenuView$1.onReceive(MenuView.java:56) 03-07 12:57:59.394: ERROR/AndroidRuntime(803): at 
+4
source share
3 answers

I think the problem is how you complete activity B. When you complete Activity B, the result from B is sent back to A, and the activity disappears. C cannot know that he should return his result A, since he knows only about activity B.

My suggestion for you is that you should use BroadcastReceiver in step A. Then in step C you can send a broadcast with the data you want to receive via A:

In A:

 IntentFilter filter = new IntentFilter("my.action"); BroadcastReceiver receiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { if(intent.getAction().equals("my.action")) { // Do my stuff } } } registerReceiver(receiver, filter); 

In C:

 Intent data = new Intent("my.action"); data.putExtra("point", point); data.putExtra("noqs", noqs); sendBroadcast(data); 
+5
source

When you complete activity B, why are you using the new Intent instead of the getIntent() method?

0
source

You can use Fragment instead of actions in your game and control the state transition in the FragmentManager instead of trying to use the activity stack. It seems that it may be a little easier to manage.

0
source

Source: https://habr.com/ru/post/1342612/


All Articles