Reputation: 337
I know it's weird but I've faced with this problem today. I developed several applications but I have never seen anything like this. Let me explain more; I have a class which is the parent of all Activities. I named it ActivityEnhanced.
public class ActivityEnhanced extends AppCompatActivity implements INetConnection.Callback{
public static INetConnection mNetWork;
private static DataPacket dataPacket;
@Override
protected void onResume() {
super.onResume();
G.setCurrentActivity(this);
if(dataPacket == null){
dataPacket = new DataPacket();
}
if(mNetWork == null){
mNetWork = new INetConnection(this);
mNetWork.connectToNetwork();
}
}
.....
.
.
public void sectionRequestDone() {}
}
I have two children Activities A and B, I want to login to activity B when I receive confirmation code in activity A. there is one method in both activity which is common between them and I'm using this method to trig child class when a specific event happens. [sectionRequestDone()]
public class A extends ActivityEnhanced {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
....
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = username.getText().toString();
String pwd = password.getText().toString();
login(userInfo); // this method is in parent class
}
});
}
@Override
public void sectionRequestDone() {
goToActivity();
}
private void goToActivity(){
Intent intent = new Intent(this, B.class);
startActivity(intent);
this.finish();
}
}
Class B;
public class B extends ActivityEnhanced {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.b);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clearSections();
sectionRequest();
}
});
fillAdapter();
}
@Override
public void sectionRequestDone() {
fillAdapter();
alphaAdapter.notifyDataSetChanged();
}
}
now he question is, whenever I click on btn in class B method sectionRequestDone() in class A is calling but I want it works in class B. This is so weird because I finished class A. Is there any solution for this problem?
Thanks in advance.
Upvotes: 0
Views: 65
Reputation: 337
First of all I have to thanks all who helped me. After a few hours I've found the solution.
Solution:
If I want to use an activity as a parent in which there are some static fields, I have to set their listeners in every activity which means that I have to do that in onResume() method in ActivityEnhanced class. In the previous code all listeners for INetConnection called just in class A and these listeners didn't called again in class B so when sectionRequestDone() method called it trigged in class A not B.
I removed (implements INetConnection.Callback) from ActivityEnhanced and changed the code like this:
public class ActivityEnhanced extends AppCompatActivity{
public static INetConnection mNetWork;
private static DataPacket dataPacket;
@Override
protected void onResume() {
super.onResume();
G.setCurrentActivity(this);
if(dataPacket == null){
dataPacket = new DataPacket();
}
if(mNetWork == null){
mNetWork = new INetConnection(this);
mNetWork.connectToNetwork();
}else{
mNetWork.setOnCallbackListener(new INetConnection.Callback(){
@Override
public void onSocketNull() {}
@Override
public void onConnectionSuccessful() {}
@Override
public void onDataReceive(Byte[] data) {
.
.
.
sectionRequestDone();
}
);
}
}
.....
.
.
public void sectionRequestDone() {}
}
Upvotes: 1