0

In the App I'm developing I've got 2 Activities, ActA and ActB.

ActA is the first one to be displayed.

We can say ActA works like a SplashScreen.

Inside ActA i retrive some data which i need in ActB for performing some tasks.

When this data get retrived i can call an Intent which perform the switch to ActB.

Here's the problem: ActB extends ActA becouse it needs ActA retrived data to perform his tasks.

Probably becouse of that, in the moment ActB gets called, ActA method "OnCreate" gets called to.

This create a loop becouse ActA starts retriving other data and calls again ActB. So my app Crash.

How do i forbid ActA to start a second time?

L. Gangemi
  • 3,110
  • 1
  • 22
  • 47
  • You should use an interface to pass on the data, or pass the data through intent, instead of doing it. – Anmol317 Apr 19 '17 at 09:58
  • `Here's the problem: ActB extends ActA becouse it needs ActA retrived data to perform his tasks.` Well, indeed it is the problem. You should be passing that parameter to `ActB` and not extend `ActA`. – azizbekian Apr 19 '17 at 09:58
  • Can you post your Activities? – Johny Apr 19 '17 at 09:58
  • It's better to use intent to send data from ActA to ActB so once the ActB gets started ActA will not be called. – TysonSubba Apr 19 '17 at 09:59
  • The problem is that the data i need to send is a BLE device and all his characteristiches end services. – L. Gangemi Apr 19 '17 at 10:11

4 Answers4

1

If you don't want to exchange data between Activity you can do this.
Create a base activity like this:

    class BaseActivity extends Activity{
      String data;
    }

    Now class ActA extends BaseActivity{
    //you can put value to data directly
    data="ABCD";
    }

    class ActB extends BaseActivity{
    //Here you can access the data string directly
    }
Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
Satyam Anand
  • 377
  • 1
  • 8
0

You can pass your retrieved data from ActA to ActB via an intent, like this

Intent i = new Intent(getActivity(), ActB.class);
i.putExtra("String1", "foo");
i.putExtra("Integer1", "1337");
startActivity(i);

and then in your ActB you reviece the data like this:

Intent i = getIntent();
String foo = i.getStringExtra("String1");
Integer leet = i.getStringExtra("Integer1");

If you want to pass a custom object, please reffer to How to send an object from one Android Activity to another using Intents?

Community
  • 1
  • 1
Malik
  • 878
  • 2
  • 9
  • 23
  • I'll try, the problem is that the data i need to send are not simple integers or String but a BLE device with is services and characterisiches – L. Gangemi Apr 19 '17 at 10:12
  • You forget to mention that OP's approach is completely wrong. This is half an answer at most – Tim Apr 19 '17 at 10:13
  • @L.Gangemi like i said: if you want to pass an bigger object, not only simple varibles click the link at the bottom of the answer – Malik Apr 19 '17 at 10:18
  • Android BLE Device can't be serialized – L. Gangemi Apr 19 '17 at 10:20
0

This is happening because ActB's onCreate() method is calling super(), it causes calling ActA's onCreate() method again and again. So, it's better not to extend ActA in ActB. You can pass these values through passed intent from ActA to ActB.

Mansuu....
  • 1,206
  • 14
  • 27
0

First of all, you need to brush up your OOP concepts.

As you have said ActA is more like a SplashScreen, so I assume ActB is the MainScreen. You should not extend ActA to create ActB. There is no Is-A relationship between them.

Here's the problem: ActB extends ActA because it needs ActA's retrieved data to perform it's tasks.

You have extended ActA for the very wrong reason.

Going forward, I assume (as you haven't provided any code) that you have written the code which starts ActB in ActA's onCreate() method, that's why the issue

This creates a loop because ActA starts retrieving other data and calls again ActB. So my app crashes.

please note, due to your structure, once ActB is launched for the first time, it is the one which is retrieving the data and invoking itself and not ActA.

please remove the inheritance and use Intent.putExtra(key, value) methods (docs) for passing the data from ActA to ActB.

e.g. in ActA after retrieving some_data

Intent i = new Intent(ActA.this, ActB.class);
i.putExtra("some_key", some_data);
ActA.this.startActivity(i);

and in ActB where the data is needed

Intent i = getIntent();

and retrieve the some_data from i using appropriate API Intent.get<TYPE>Extra()e.g. i.getIntExtra("some_key") (docs)

It will work.

Please refer @Malik's answer also.

Rupesh
  • 3,415
  • 2
  • 26
  • 30