Android Facebook Integration without Fragments - android

I am creating a social app and would like to have integration with Facebook so I can grab information on Friends, User Info, to Post on Users Wall and Upload a Picture to Facebook. I have followed the tutorials on the Facebook website however they are all using Fragments. I would prefer to not use fragments if at all possible. I'm sure that Facebook wouldn't tie their API around the use of Fragments as that could shut out some people.
I understand the clear answer is to use the newest API with fragments however I would rather not do that for the following reasons:
I don't see why you wouldn't be able to do it without Fragments.
I don't entirely understand the point for Fragments and why they are becoming increasingly more popular.
I have most of the application implemented without using Fragments and would like to implement Facebook login/use at this point in development.
That being said, how can I go about doing this without the use of Fragments?
Why are Fragments becoming increasingly popular?
If the answer is to use the Facebook API with Fragments, is there an "easy" way to alter the app I've already created, that uses Activities, so that it uses Fragments?
Cheers,
Jake

I'm using Fragments, but I've implemented the Facebook Login button using an activity.
I just declared the button in layout
<com.facebook.widget.LoginButton
android:id="#+id_login/btEntrarFacebook"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:background="#drawable/shape_bg_bt_azul"
android:layout_below="#+id_login/btEntrar"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:textColor="#ffffff"
android:textSize="16dp"
android:typeface="serif"
android:text="f | Entre com o Facebook"/>
and in the activity:
btEntrarFacebook = (LoginButton) findViewById(R.id_login.btEntrarFacebook);
The SDK examples comes with both Activity and Fragments implementations. The tutorials aren't clear enough, but the examples are way better than own Facebook manuals.

There are many sites which will tell you about the advantages of using fragments over activities. One very important reason is that as the screen sizes vary a lot now with different devices, so using fragmnets we can utilize diffeerent screen sizes very well.
Changing activities to fragmnets is very simple as the life cycle and apis of both these components are alomost same. may be you can change an activity to fragment in 15-20 minutes :)
Give it a shot ..

With fragments the concepts is like - There is a FragmentActivity. It is like normal fragmnets which in turn holds different fragmnets. I will try to explain you wih an example which is there in android developer site. Suppose we are working on tabltes. We can have a layout whihcan have left pane and right pane. Bothe panes are two different fragmnets. left pane will contain menu items and on clickin any item, right pane will show. since tablet screen is large we can show both pane on the screen and it will be easy for the user to use. May be like news application. But this design wont look good on small phone screens. in such cases fragmnets panes will automatically get divided into two screens and you can swipe between them. each pane will occupy full screen now. so without you doing anything, fragment takes care of screen sizes to adjust their layout. also normally in portrait mode, they are shown as tabs but in horizontal mode, they become drop down..

If you want a sample reference on how to switch your activities to fragments. I'm adding my example activity and fragments.
// Now this is the Activity class which I have to convert to fragments
TestActivity.java
public class TestActivity extends Activity implements OnClickListener {
private static EditText edittext;
private static Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
edittext = (EditText) findViewById(R.id.et_price);
button = (Button) findViewById(R.id.bt_bold);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.equals(button)) {
edittext.setText("Arshad's Test App");
}
}
}
Now for converting the TestActivity to a Fragment we need to make it in two classes. TestActivity.java and TestFragment.java
// Now the TestActivity will extend the FragmentActivity instead of Activity
public class TestActivity extends FragmentActivity {
private TestFragment testFragment_object;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
// Add the fragment on initial activity setup
testFragment_object = new TestFragment();
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, testFragment_object).commit();
} else {
// Or set the fragment from restored state info
testFragment_object = (TestFragment) getSupportFragmentManager()
.findFragmentById(android.R.id.content);
}
}
}
Now the Fragment class will contain all the code of the layouts
and actions to be performed in class which was contained in the Activity class earlier.
TestFragment.java
public class TestFragment extends Fragment implements OnClickListener {
private static EditText edittext;
private static Button button;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_first, container, false);
edittext = (EditText) view.findViewById(R.id.et_price);
button = (Button) view.findViewById(R.id.bt_bold);
button.setOnClickListener(this);
return view;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.equals(button)) {
edittext.setText("Arshad's Test App");
}
}
}

Related

Programmatically show Layout to preface an activity

What I want to do:
I want to have multiple activities each prefaced with a page explaining to the user what the activity is about.
What I'm currently doing:
So my main class BaseModuleActivity extends Activity and I am trying to write a function called showTutorial() which will explain the next steps to the users.
Here is my attempt in doing so:
public void showTutorial(String title, String explanation){
setContentView(R.layout.tutorial_screen);
TextView tv1 = (TextView)findViewById(R.id.tutoTextTitle);
tv1.setText(title);
TextView tv2 = (TextView)findViewById(R.id.tutoTextExplanation);
tv2.setText(explanation);
findViewById(R.id.tutoButton).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
//remove the tutorial's view
findViewById(R.id.tutoLayout).setVisibility(View.GONE);
}
});
}
And this method is called in the following:
public class myFirstActivity extends BaseModuleActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
//First show tuto
super.showTutorial(getString(R.string.upTitle),getString(R.string.upExplanation));
//TODO then actually do the activity stuff
/*
(findViewById(R.id.next_button)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
*/
}
}
Problem:
I think the problem is mainly conceptual. I don't know the best approach to do this and the approach I'm taking is not working.
What I'm doing is not working because the view just become empty. I thought setting the visibility of the linearLayout to gone would make it disappear and the actual activity will able to take place.
What I need:
I need to understand if I can do what I want with my approach or not? Or what approach should I take.
I found some similar questions. However, the answer to these questions didn't seem to fit my problem.
I also looked into layout inflater and fragment, but layout inflater seem to be more for listView and fragment uses layout inflater.
Well, there are some approaches to show a guide for your activity (or application).
First one, and probably the easiest, is to show a dialog/TextView when user enters an activity and explain the activity guide in that dialog/TextView using plain text. From your explanation, I think this one is what your are trying to do.
Second one is to use something like slides with pictures to explain about your activity (like Google Sheets application).
Third one is to explain each control in your activity separatly by highlighting them (similar to how Go Launcher explains its feature on first launch)
You can find more info in below links:
How to implement first launch tutorial like Android Lollipop apps: Like Sheets, Slides app?
Android - first launch interactive tutorial
Seems that what you want is actually an introduction. Take a look at this project:
https://github.com/rubengees/introduction
From each introduction page you can launch the correspondent activity.

Fragment interaction listeners in FragmentPagers

I am creating a tabbed application using a FragmentPagerAdapter and android.support.v4.view.ViewPager. In order to follow best practices and separation of concerns I have separated my app into several different fragments. Each of the fragments has an interaction listener that requires an Activity to subscribe to. Since I only have one activity in the entire app do I have to make the parent (tabbed navigation) activity implement all the listeners in the entire app? This seems like it would be bad practice and create one large monolithic Activity class that controls the flow of everything.
I have three fragments inside of another fragment that I use as a home page tab. The home page fragment implements the interfaces of the three sub fragments. The problem is that the home page fragment is not an Activity so the sub fragments throw an exception on onAttach.
What am I missing? How can I implement fragment listeners in a tabbed application without making one large and messy Activity class
After researching further with different keywords I found a good answer here: https://stackoverflow.com/a/23144683/2435006
They key was to make an onAttachFragment(Fragment f) method rather than using the onAttach(Activity a) and calling it in the onCreate method.
Here is the example from the answer above:
public void onAttachFragment(Fragment fragment)
{
try
{
mListener = (OnInteractionListener) fragment;
} catch (ClassCastException e)
{
throw new ClassCastException(fragment.toString() + " must implement OnInteractionListener");
}
}
#Override
public void onCreate(Bundle savedInstanceState)
{
Log.i(TAG, "onCreate");
super.onCreate(savedInstanceState);
this.mContext = getActivity().getApplicationContext();
onAttachFragment(getParentFragment());
....
}
I think you need a good sample related to Fragments. Here is a sample with detailed explanations # ViewPager with FragmentPagerAdapter. The listeners are at onCreateView method at the Fragment. You don't want to set a bunch of listeners in the Activity.

How start the FragmentActivity by displaying the middle fragment's view first

I just started learning android.Hope this isn't silly question.Here it is
My FragmentAdapter has a List of Fragments,Say 3 Fragments 1,2,3.
I have set this FragmentAdapter for my ViewPager using myViewPager.setAdapter(myFragmentAdapter);
So this application kicks off by displaying view of Fragment 1.How do I start the Application by displaying Fragment 2's View first.
You can use setCurrentItem(index) to switch your current page.
In this case, you would want to do:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ... init code ...
myViewPager.setCurrentItem(1);
}
See the ViewPager docs for more information. You may wish to use the variation of this method which allows you specify where there should be a smooth scroll, and set it as false, otherwise the user may see the animation the moment the app opens and it could be confusing. For example,
myViewPager.setCurrentItem(1, false);

Android Fragment vs. Activity Group

I am working on an Android App where I want to implement an Activity Group for each tab. But since Activity Group is deprecated I have to use Fragments. I googled the last days and did some research on that topic but I still don't get it. The graphic below describes what I want to do. Also I'm coming straight from iOS and I need some feedback about my theories.
As you can see every Fragment consists of a WebView Fragment. When the user clicks on a link in that WebView Fragment - the request gets caught and a new Fragment is replaced which again holds a WebView and loads the link which was clicked in the previous Fragment. At a point the user decides to go back to the first Fragment and presses the back button.
The Fragments should pop off the stack in the reverse order until he again sees the first one. Ideally each Fragment should save his instancestate, so that when the user goes back the WebView doesn't need to load the site again.
I've come across ActionBar Sherlock which provides an example about Fragments Tabs. There is also an Example of an Fragment Stack. It would be ideal to combine these two examples, so that each tab consist of a Fragment Stack.
My code structs look like the following:
My TabHost Fragment
public class MyActivity extends SherlockFragmentActivity {
static TabHost mTabHost;
static TabManager mTabManager;
public static int THEME = R.style.Theme_Sherlock_Light;
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(THEME); //Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_tabs);
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();
mTabManager = new TabManager(this, mTabHost, R.id.myRealTabContent);
// adding some Fragment Tabs
mTabManager.addTab(mTabHost.newTabSpec(tabNames[i]).setIndicator(tabNames[i]),
FragmentStackSupport.CountingFragment.class, bundle);
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("tab", mTabHost.getCurrentTabTag());
}
// defining the Tab Manager
How would the Fragment Stack for each Tab should look like?
Currently I have a Fragment Activity with a Fragment inside of each tab. But I don't achieve the logic I need.
Here is my code: My Fragment with WebView inside
I would be glad for some feedback and hints (or are there any examples on the web?). I also have some concerns about the memory - when the user clicks and clicks and clicks and the memory has to hold each Fragment in the stack.
Cheers.
I can't think of many good reasons to create a new fragment each time the user clicks a link. Is there a reason that you're trying to do that? If you simply return false from shouldOverrideUrlLoading() instead of creating a new fragment, the WebView will load the new page, and if you want to go back, you can use the canGoBack() and goBack() methods. To handle back button presses, you can override onKeyDown in your activity, and do something like:
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack() {
myWebView.goBack();
return true;
}

Android Inline Activity

In Android, the TabHost object renders activities in a type of inline way. I'm wondering if there's any way I can do a similar type of thing, without using the tab-host. Suppose, i want to have a toolbar or sliding drawer that allows me to switch between the activities in the same way that the TabHost does this. In other words, I'd like to render an activity inline inside of another activity, sort of like an iframe for activities...
Basically you need to play with LocalActivityManager and the ActvityGroup class:
Suppose you have your DashBoard class:
public class Dashboard extends ActivityGroup implements View.OnClickListener {
super.onCreate(savedInstanceState);
//Your view with the activity launcher buttons on the bottom for instance
setContentView(R.layout.frame);
#Override
public void onClick(View v) {
Intent intent = new Intent().setClassName(context,YourActivity.class);
intent.setAction(Intent.ACTION_VIEW);
LocalActivityManager localActivityManager = getLocalActivityManager();
final Window w = localActivityManager.startActivity("uniqueID", intent);
final View wd = w != null ? w.getDecorView() : null;
//the content of your activity goes here
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.tabcontent);
frameLayout.removeAllViews();
frameLayout.addView(wd);
}
}
This may not be exactly loading separate Activities, but...
Instead of Activities, you could achieve that functionality from a user's perspective by by dynamically loading layouts inside a single Activity. That way you could have a slider and update the layout(s) on screen as needed.
No and even use of activities in tabs is discouraged in favor of views. You can do other searches here or on the android google groups to read why.
If you must have separate activities you should start them the proper way with Intents and let Android manage their lifecycle or do tabs with a view per tab.

Categories

Resources