public class MyClassActivity<T> extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// todo..
}
It is possible?
How startActivity with T object?
Related
I instantiate a static property at the beginning of Activity's onCreate(), but some of my users get NullpointerException when I try to get it in MainActivity.
In my opinion, you code should be
public class TestActivity extends AppCompatActivity {
public static String mValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
mValue = "my_value";
}
}
The value TestActivity.mValue should be null util the onCreate executes.You can init the mValue like this
public class TestActivity extends AppCompatActivity {
public static String mValue;
static {
mValue = "my_value";
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
I have an issue , need your help , my question is : I have a multiple activity , so i have created one abstract class which hold the view . Now my question i am able to pass the view to the Base Activity but how to finds view id in Child Activity Class.
I Tried like this :
public abstract class BaseActivity extends Activity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResourceId());
}
public abstract int getLayoutResourceId();
}
My Activity Class:
public class ServiceActivity extends BaseActivity {
Button startSerice_btn, stopService_btn;
MyService myService;
#Override
public int getLayoutResourceId() {
return R.layout.service_xml;
}
}
You can access your views after the super.onCreate() call in the ServiceActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Access them here
}
I think you no need to set content view in BaseActivity like below :
public abstract class MyAppBaseActivity extends AppCompatActivity implements View.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
And you just extends Activity like below :
// for MyCustomActivity1
public class MyCustomActivity1 extends MyAppBaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public void onClick(View view) {
}
}
// for MyCustomActivity2
public class MyCustomActivity2 extends MyAppBaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public void onClick(View view) {
}
}
Im interested if i can to set some common listeners inside main activity class? For my project i use FirebaseAuth, so i would like to init it in MainActivity onCreate(), setup needed listeners in onStart() and onStop(), and then inherit that class in every other activity class.
Some code to please you :]
MainActivity class [parent]:
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
protected FirebaseAuthentication firebaseAuthentication;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
firebaseAuthentication = new FirebaseAuthentication(FirebaseAuth.getInstance(), FirebaseDatabase.getInstance());
}
#Override
protected void onStart() {
super.onStart();
firebaseAuthentication.addAuthStateListener();
}
#Override
public void onStop() {
super.onStop();
firebaseAuthentication.removeAuthStateListener();
}
}
AuthActivity class [child]:
public class AuthActivity extends MainActivity implements FirebaseAuthentication.OnUserAuthListener {
#BindView(R.id.viewPager) LockableViewPager viewPager;
private String userUID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_market);
ButterKnife.bind(this);
firebaseAuthentication.setOnUserAuthListener(this);
firebaseAuthentication.isSingedIn(); // check if user is singed in
}
#Override
// response for firebaseAuthentication.isSingedIn() above
public void onAuthSuccess(String userUID) {
this.userUID = userUID;
}
#Override
// response for firebaseAuthentication.isSingedIn() above
public void onAuthFailure(String message) {
snackbar(message);
Intent intent = new Intent(this, AuthActivity.class);
startActivity(intent);
finish(); // TODO mb should to delete it
}
}
Can this implementations bring me errors (maybe NullPointerExeption or what unexpectedly in future)?
Would be great if you provide me some sources to read/watch.
Thank you.
Perfect example of abstraction, but not really a question.
You will not get any nullpointers or other errors by implementing it like this.
I have created an activity called ButtonActivity that has a lot of buttons and listeners. I want to create another activity TwoButtonsActivity to extend ButtonActivity so that the listeners I created can be resused.
TwoButtonsActivity is similar to ButtonActivity but with small changes.
Is this possible?
When I execute the code, I find that the extended activity do not respond to button click.
Here is the base activity:
public class ButtonActivity extends Activity {
int count = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
button.setText("Got Pressed:" + ++count);
}
});
}
}
Below is the extends Activity:
public class TwoButtonsActivity extends ButtonActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
you can have a activity with listeners same below
public class ButtonActivity extends Activity {
int count = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
}
public void mylistener1(View v)
{
switch(v.getid()){
case R.id.button:{
//do somthings
}break;
}}
public class TwoButtonsActivity extends ButtonActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
and in layout of TwoButtonsActivity(main) you set in tag of your button android:onclick="mylistener1"
and you should set to any button or view that you want it use this listener
Is it possible to set the titlebar in every activity programmatiaclly from one particular activity?
I can set the titlebar of a particular activity programmatically from within that activity, but subsequent activities have the string set from the manifest thereafter.
To set a title I use the following from within the activity. I would like every activity set like this in the application.
setTitle(carerName + " is now logged in");
you can write base activity and call a method of super .
public class DerivedActivity extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...}
}
public class DerivedActivity2 extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...}
}
public class BaseActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(carerName + " is now logged in");
}
}