Reputation: 3290
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?
Upvotes: 0
Views: 48
Reputation: 377
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
}
Upvotes: 1
Reputation: 3505
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.
Upvotes: 0
Reputation: 1226
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.
Upvotes: 0
Reputation: 904
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?
Upvotes: 0