How to work with CordovaActivity class? - android

I'm using cordova in my project and I want to get access to some urls.
I have a class which extends CordovaActivity class. in Myclass, onCreate() function will be added to myClass automatically when cordova build the project.This class will be generated whenever cordova build the broject.
My problem is that my new code will be always disappear in that class when I run cordova build. I also reffer to this class in my create Notificatio function in GCMIntentService class.
Any idea about where I should add my new code to get access to urls?
here is my code:
public class Myclass extends CordovaActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
super.loadUrl(Config.getStartUrl());
}
// here is my new code that intend to loud a specific page
#Override
public void onResume() {
super.onResume();
String url = Config.getStartUrl();
Intent intent = getIntent();
if (intent != null) {
String s = intent.getStringExtra("HTML_PAGE");
if (s != null) url = s;
}
super.loadUrl(url);
}
// here is my new code
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
}

Related

Can setContentView be replaced by android-annotations when espresso is used?

I have espresso test which verifies that text is displayed:
public class InformationActivityTests {
#Rule
public ActivityTestRule<InformationActivity_> mInformationActivityTestRule =
new ActivityTestRule<InformationActivity_>(InformationActivity_.class) {
#Override
protected Intent getActivityIntent() {
Intent i = new Intent(getTargetContext(), InformationActivity_.class);
i.putExtra("INFORMATION", "Espresso");
return i;
}
};
#Test
public void textIsDisplayed() {
onView(withText("Espresso")).check(matches(isDisplayed()));
}
}
This test passes when Activity has following code:
#EActivity
public class InformationActivity extends AppCompatActivity {
#ViewById(R.id.information_view)
TextView informationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information);
Intent intent = getIntent();
String information = intent.getStringExtra("INFORMATION");
informationView.setText(information);
}
}
but fails when I "move" setContentView to #EActivity annotation:
#EActivity(R.layout.activity_information)
public class InformationActivity extends AppCompatActivity {
#ViewById(R.id.information_view)
TextView informationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String information = intent.getStringExtra("INFORMATION");
informationView.setText(information);
}
}
Error is:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.edu/com.edu.InformationActivity_}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
Am I doing something wrong or is it an issue with espresso / android-annotations?
I've checked code generated by android-annotations when using #EActivity(R.layout.activity_information) and this is how onCreate looks like in generated class (InformationActivity_):
public void onCreate(Bundle savedInstanceState) {
OnViewChangedNotifier previousNotifier = OnViewChangedNotifier.replaceNotifier(onViewChangedNotifier_);
init_(savedInstanceState);
super.onCreate(savedInstanceState);
OnViewChangedNotifier.replaceNotifier(previousNotifier);
setContentView(R.layout.activity_information);
}
the problem is that it first calls super.onCreate (so onCreate from InformationActivity) where I handle intent and TextView and then it calls setContentView and this can't work like that.
The solution for this is what Be_Negative suggested, so using #AfterViews annotation. It also simplifies code a bit (I could remove onCreate method):
#EActivity(R.layout.activity_information)
public class InformationActivity extends AppCompatActivity {
#ViewById(R.id.information_view)
TextView informationView;
#AfterViews
void handleIntent() {
Intent intent = getIntent();
String information = intent.getStringExtra("INFORMATION");
informationView.setText(information);
}
}
Your problem is that you are never initializing informationview
public class InformationActivity extends AppCompatActivity {
#ViewById(R.id.information_view)
TextView informationView; //<-- Null
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String information = intent.getStringExtra("INFORMATION");
informationView.setText(information); //<--STILL NULL
}
}
So you first will have to initialize that view.

Put common listeners inside MainActivity class

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.

Split code to two classes

I have a class with a lot of code. I want to put some of the methods into another class, but load them from the class that i already have. I don't want to change the contentview either. I tried
Class1:
public class Class1 extends Activity {
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState;
Intent intent = new Intent (this, Class2.class);
startActivity(intent);
}
}
Class2:
public class Class2 extends Class1 {
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
method2();
}
}
public void method2 {
Log.e("Error", "Wut?")
}
When i run that code, the app just displays the error message over and over. The app is probably running the "method2" method over and over again... I just want to run the "method2"once. I don't want the code in the same file either, because there is gonna be a lot of code...
All you have to do is call the class2 in a object Class. Remember that is a java class so. Call it!
Class2 newClass = new Class2();
newClass. method2();
The class2 is not a activity, is only a class for has all your methods.. It is would not extends of nothing!
As I see no sense in your approach here's a workaround.
public class Class1 extends Activity {
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState;
if(!this instanceof Class2)
{
Intent intent = new Intent (this, Class2.class);
startActivity(intent);
}
}
}

Can I use ApplicationContext to log events instead of Activity context for facebook app events?

I am using facebook sdk to track app installs and log events.I have 4 activities in my app. According to the documentation, I am activating and deactivating in every activity.
public class MyActivity extends Activity {
private FacebookSingleton fb = FacebookSingleton.getInstance();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//some event occurred
Bundle params = new Bundle();
params.putString("SEARCH QUERY", query);
fb.trackEventForFb("PRODUCT SEARCH", params);
}
#Override
protected void onResume() {
super.onResume();
AppEventsLogger.activateApp(this);
}
#Override
protected void onPause() {
super.onPause();
AppEventsLogger.deactivateApp(this);
}
public class FacebookSingleton {
private static MyApplication appInstance;
private static FacebookSingleton instance;
private FacebookSingleton() {
};
public static void setupFb(MyApplication myAppInstance) {
if (instance == null) {
instance = new FacebookSingleton();
appInstance = myAppInstance;
}
}
public void trackFacebookEvent(String event,Bundle parameters) {
AppEventsLogger logger = AppEventsLogger.newLogger(appInstance);
logger.logEvent(event, parameters);
}
And this is my application
public class MyApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
FacebookSdk.sdkInitialize(this);
FacebookSingleton.setupFb(this);
}
}
When I log events, can I create a singleton and pass that context instead of using activity context to log events like the code aboce? Or do I need to pass the activity context only according to the documentation?
It will work, but you may not get the full information. For certain events we will try to get things like the Activity name from the context. It's guarded so that if the Context is not an Activity, it will not fail, but you may not get the full information passed along.

How to check state of app optimally?

I develop android app. It needs save Internet data and login/password pair. I need to check that in every activity. How to do that optimally? My solution:
I create the BaseActivity class:
public class BaseActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkRequirements();
}
private void checkRequirements() {
if (needToCheckInternetConnection())
checkInternetConnection();
//check other requirements
}
private void checkInternetConnection() {
if (!Updater.getInstance(context).checkInternetConnection()) {
Bundle data = new Bundle();
data.putSerializable(SplashScreenActivity.FIELD_ACTION, SplashScreenActivity.Action.SHOW_TEXT);
data.putString(SplashScreenActivity.FIELD_TEXT, getString(R.string.splashScreenInternetNotAvailable));
changeActivity(SplashScreenActivity.class, data);
}
}
protected void changeActivity(Class<?> goTo, Bundle data) {
Intent intent = new Intent(context, goTo);
if (data != null)
intent.putExtras(data);
startActivity(intent);
finish();
}
}
And I extends all activity classes from this.
But finish() not working. So, how to change that architecture or force to work the finish() method?

Categories

Resources