Snippets and actions - where can I put my application logic?

I created my idea of ​​how I want it to look. It has 1 image, input field and button. I want to load another action at the click of a button. I am confused why there are fragments and actions. I am new to the Android world (from iOS).

I understand that actions are similar to ViewControllers, but I'm not sure I understand what a fragment is.

Where can I put event handling?

package com.phppointofsale.phppointofsale; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.os.Build; public class StoreUrlActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_store_url); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new StoreUrlFragement()).commit(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.store_url, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. */ public static class StoreUrlFragement extends Fragment { public StoreUrlFragement() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_store_url, container, false); return rootView; } } } 
+7
android android-fragments
source share
1 answer

Firstly, I would recommend reading this Fragments . Pay particular attention to the section of the created fragment that includes the fragment life cycle diagram. The second download and compilation of this sample application , an effective navigation application will help you understand how different fragments work in tandem and even implements an action bar.

To answer your question more or less, a fragment can be considered as a separate class. After calling this particular fragment, you can call functions from this class.

Fragment switch switch

This is a sample code to show you what I mean.

  public Fragment getItem(int i){ switch (i) { case 0: // The first section of the app is the most interesting -- it offers // a launchpad into the other demonstrations in this example application. return new LaunchpadSectionFragment(); case 1: return new BluetoothClass(); default: // The GPS section of the app . Fragment fragment = new DummySectionFragment(); Bundle args = new Bundle(); args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1); fragment.setArguments(args); return fragment; } } 

In this case, each fragment for me represented a class that was implemented on a separate tab, and each tab had its own functionality. One of the key benefits of snippets is that you can run individual actions without first completing one action.

In addition, each fragment is an extension of the java.lang.Object library. Thus, it has all these features + additional. I would read this one . Finally, it would be nice to have separate XML files for each fragment, then you can display them separately when calling the fragment.

One more code

Each fragment will / may have this

 public void onActivityCreated(Bundle savedInstanceState){ super.onActivityCreated(savedInstanceState); // Do stuff on creation. This is usually where you add the bulk of your code. Like clickListners View rootview = inflater.inflate(R.layout.xml_the_fragment_uses container,false); rootview.findViewById(R.id.your_id).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Do something } }); } public void onStart(){ super.onStart(); Toast.makeText(getActivity(), "Fragment started",Toast.LENGTH_SHORT).show(); } public void onResume(){ super.onStart(); Toast.makeText(getActivity(), "Fragment Resumed",Toast.LENGTH_SHORT).show(); } public void onStop(){ super.onStart(); Toast.makeText(getActivity(), "Fragment Stoped",Toast.LENGTH_SHORT).show(); disableBT(); } 

Remember that these functions are taken from the fragment life cycle that I mentioned earlier.

Hope this gave you some idea of ​​the fragments. Also, don't forget to read this , as many features use the v7 application compatibility library. Including fragment manager .

+3
source share

All Articles