It is necessary to call FacebookSdk.sdkInitialize() each time? - android

I call FacebookSdk.sdkInitialize(getApplicationContext()) in my GlobalActivity.
public class GlobalActivity extends Application {
#Override
public void onCreate() {
super.onCreate();
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
}
}
Do I need to call it again each time I am using facebook sdk?
I have a Fragment with a LoginButton.
public class LoginFragment extends Fragment{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
}
}

Simple way is Initialize once in Application class thats enough. when new activity created FacebookSdk automatically Initialized.
import com.facebook.FacebookSdk;
import com.facebook.appevents.AppEventsLogger;
public class ApplicationName extends Application {
#Override
public void onCreate() {
super.onCreate();
// Initialize the SDK before executing any other operations,
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
}
}

Related

What`s wrong -> cannot be instantiated

import android.app.Application;
import com.estimote.coresdk.observation.region.Region;
import com.estimote.coresdk.service.BeaconManager;
import java.util.UUID;
public class MyApplication extends Application {
private BeaconManager beaconManager;
#Override
public void onCreate(){
super.onCreate();
beaconManager = new BeaconManager(getApplicationContext());
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
#Override
// 비콘 uuid,major minor
public void onServiceReady() {
beaconManager.startMonitoring(new Region());
}
});
}
}
this is my code
when I use this code
-beaconManager.startMonitoring(new Region());-
get error named cannot be instanted
how can I fixed??
http://loveiskey.tistory.com/207
this is the site where I referd
Estimote Region is an interface. You cannot instantiate interfaces. You need to instantiate some class that implements that interface instead.

Communication between BroadcastReceiver and Activity - android

I have a broadcast receiver in my app which is fired every time the user gets an incoming call. Now, when it happens, I need the broadcast receiver to invoke a specific method in a specific activity. Now, I tried to make this method static and therefore available, but something tells me it is a very bad idea.
Accordingly, I tried to instantiate the broadcast receiver inside my activity without declaring it in my manifest but the problem is - when the app is off, the activity dosn't exist and therefore I can't invoke my method.
So my question is - How can I invoke this method when the broadcast receiver is fired up, without making it "public static"?
Here is my activity code(I have deleted the irrelevant parts)
package com.silverfix.ringo.activities;
import com.silverfix.ringo.R;
import com.silverfix.ringo.activities.fragments.DataManagerFragment;
import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
public class RingtonesActivity extends Activity{
private DataManagerFragment dataManagerFragment;
private IntentFilter filter;
private BroadcastReceiver phoneCall;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ringtones);
ActionBar ab = getActionBar();
ab.setDisplayShowTitleEnabled(false);
ab.setDisplayHomeAsUpEnabled(true);
dataManagerFragment = new DataManagerFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(dataManagerFragment, "DataManagerFragment");
ft.commit();
filter = new IntentFilter();
filter.addAction("android.intent.action.PHONE_STATE");
phoneCall = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
dataManagerFragment.act();
}
};
registerReceiver(phoneCall, filter);
}
}
You can use observers , like
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
ObservableObject.getInstance().updateValue(intent);
}
}
public class MainActivity extends Activity implements Observer {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ObservableObject.getInstance().addObserver(this);
}
#Override
public void update(Observable observable, Object data) {
Toast.makeText(this, String.valueOf("activity observer " + data), Toast.LENGTH_SHORT).show();
}
}
public class ObservableObject extends Observable {
private static ObservableObject instance = new ObservableObject();
public static ObservableObject getInstance() {
return instance;
}
private ObservableObject() {
}
public void updateValue(Object data) {
synchronized (this) {
setChanged();
notifyObservers(data);
}
}
}
Receiver can be used via manifest.
ObservableObject - must be singleton.
This might help: how can I notify a running activity from a broadcast receiver?
Also, you can try using Observers
Something like:
public class BroadcastObserver extends Observable {
private void triggerObservers() {
setChanged();
notifyObservers();
}
public void change() {
triggerObservers();
}
}
In your broadcast receiver:
#Override
public void onReceive(Context context, Intent intent) {
BroadcastObserver bco = new BroadcastObserver();
bco.change();
}
and the Activity:
public class YourActivity extends Activity implements
Observer {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BroadcastObserver bco = new BroadcastObserver();
bco.addObserver(this);
}
#Override
public void update() {
//TODO: call your desired function
}
}
If anyone needs two-way communication between a BroadcastReceiver and a Activity, I wrote this utility class which simplifies invoking Methods on each other while still being memory-safe.
https://gist.github.com/Jenjen1324/4a0c03beff827082cb641fc8fe2c4e71

Use Activity functions in no activity class

it is probably an noob question but still. I need to my main activity class will use other class to do some code. This class uses function that are from Activity like getPackageName(), new Intent etc.
So I need some help with this, for example what I need it to setContentView via class. How do I do this?
Main Acticity:
package com.example.testapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
SetContentView cn = new SetContentView();
cn.MySetContentView(R.layout.activity_main);
}
}
SetViewClass:
package com.example.testapp;
public class SetContentView
{
void MySetContentView(int activityMain)
{
setContentView(R.layout.activity_main); //no set content view if not activity
}
}
public class MainActivity extends Activity
{
private static MainActivity instance;
public static MainActivity getInstance() {
return instance;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instance=this;
}
}
public class OtherClass
{
protected void someMethod() {
MainActivity ma= MainActivity.getInstance();
//use ma methods...
}
}
If I'm getting it right, you could try having a root class extending Activity and holding your methods, and then extending it in MainActivity:
public class Helper extends Activity {
... // your methods, e.g. MySetContentView
}
and then:
public class MainActivity extends Helper {
... // any call to methods in Helper
}
This can be useful if you have a lot of activities that share the same code - you can have them all extending the same Helper class, and still being Activities.
In your case:
public class Helper extends Activity {
public void mySetContentView(int layout) {
setContentView(layout)
}
}
and then:
public class MainActivity extends Helper {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mySetContentView(R.layout.your_layout);
}

How to start Application object using android? Help me to understand this concept by adding some keys (Codes)

package com.example.activitylifecycle;
import android.app.Application;
import android.content.res.Configuration;
import android.util.Log;
public class MyApplication extends Application {
static final String TAG = "MyApplication";
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
#Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate");
}
#Override
public void onLowMemory() {
super.onLowMemory();
Log.d(TAG, "onLowMemory");
}
#Override
public void onTerminate() {
super.onTerminate();
Log.d(TAG, "onTerminate");
}
Help me to understand this concept by adding some keys (Codes).
I am new to android, What are the purpose of using Application objects?
If you want to learn Android i have to recommend the google documentation:
there is also a description of the lifecycle and the application itself
http://developer.android.com/training/index.html
and her is also a tutorial to extends Application
http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/

SuperNotCalledException running unit test

I am trying to run this Android unit test, following this tutorial ::
http://developer.android.com/resources/tutorials/testing/helloandroid_test.html
and in doing so get a SuperNotCalledException
Here's the test class code ::
package com.example.helloandroid2.test;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.TextView;
import com.example.helloandroid2.HelloAndroid2Activity;
public class HelloAndroid2Test extends ActivityInstrumentationTestCase2<HelloAndroid2Activity>
{
private HelloAndroid2Activity mActivity;
private TextView mView;
private String resourceString;
public HelloAndroid2Test()
{
super("com.example.helloandroid2", HelloAndroid2Activity.class);
}
#Override
protected void setUp() throws Exception
{
super.setUp();
mActivity = this.getActivity();
mView = (TextView) mActivity.findViewById(com.example.helloandroid2.R.id.textview);
resourceString = mActivity.getString(com.example.helloandroid2.R.string.hello);
}
public void testPreconditions()
{
assertNotNull(mView);
}
public void testText()
{
assertEquals(resourceString,(String)mView.getText());
}
}
The class I'm actually testing ::
package com.example.helloandroid2;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid2Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
}
}
I've set the project API levels at 2_3_1 and am using an avd set at the same.
Am running Eclipse with ADT on Windows Vista.
All wisdom greatfully recieved. Thanks in advance.
Chris
Your onCreate() method in HelloAndroid2Activity needs to call super.onCreate(savedInstanceState);
public class HelloAndroid2Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

Categories

Resources