Advertisement
rachmadi

MainActivity Tutorial Firebase

Sep 25th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.52 KB | None | 0 0
  1. package info.rekayasa.reinfirebase;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.support.design.widget.FloatingActionButton;
  6. import android.support.design.widget.Snackbar;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.support.v7.widget.Toolbar;
  9. import android.view.View;
  10. import android.view.Menu;
  11. import android.view.MenuItem;
  12. import android.widget.TextView;
  13.  
  14. import com.google.firebase.auth.FirebaseAuth;
  15. import com.google.firebase.database.DataSnapshot;
  16. import com.google.firebase.database.DatabaseError;
  17. import com.google.firebase.database.DatabaseReference;
  18. import com.google.firebase.database.FirebaseDatabase;
  19. import com.google.firebase.database.ValueEventListener;
  20.  
  21. import java.util.HashMap;
  22.  
  23. public class MainActivity extends AppCompatActivity {
  24.  
  25.     FirebaseAuth mAuth;
  26.     // Firebase Database
  27.     DatabaseReference mRef, mRoot;
  28.     FirebaseDatabase mDatabase;
  29.     // Declare views
  30.     TextView tvFullName, tvEmail;
  31.  
  32.     @Override
  33.     protected void onCreate(Bundle savedInstanceState) {
  34.         super.onCreate(savedInstanceState);
  35.  
  36.         mAuth = FirebaseAuth.getInstance();
  37.         mDatabase = FirebaseDatabase.getInstance();
  38.  
  39.         // Check authentication
  40.         if (mAuth.getCurrentUser() == null){
  41.             startActivity(new Intent(getApplicationContext(), Authentication.class));
  42.             finish();
  43.         }
  44.  
  45.         setContentView(R.layout.activity_main);
  46.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  47.         setSupportActionBar(toolbar);
  48.  
  49.         tvFullName = (TextView) findViewById(R.id.tvFullName);
  50.         tvEmail = (TextView) findViewById(R.id.tvEmail);
  51.  
  52.         FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  53.         fab.setOnClickListener(new View.OnClickListener() {
  54.             @Override
  55.             public void onClick(View view) {
  56.                 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  57.                         .setAction("Action", null).show();
  58.             }
  59.         });
  60.  
  61.         mRoot = mDatabase.getReference();
  62.         String userId = mAuth.getCurrentUser().getUid();
  63.         mRef = mRoot.child("users").child(userId);
  64.         mRef.addValueEventListener(new ValueEventListener() {
  65.             @Override
  66.             public void onDataChange(DataSnapshot dataSnapshot) {
  67.                 User user = dataSnapshot.getValue(User.class);
  68.                 tvFullName.setText(user.getFullName());
  69.                 tvEmail.setText(user.getEmail());
  70.             }
  71.  
  72.             @Override
  73.             public void onCancelled(DatabaseError databaseError) {
  74.  
  75.             }
  76.         });
  77.     }
  78.  
  79.     @Override
  80.     public boolean onCreateOptionsMenu(Menu menu) {
  81.         // Inflate the menu; this adds items to the action bar if it is present.
  82.         getMenuInflater().inflate(R.menu.menu_main, menu);
  83.         return true;
  84.     }
  85.  
  86.     @Override
  87.     public boolean onOptionsItemSelected(MenuItem item) {
  88.         // Handle action bar item clicks here. The action bar will
  89.         // automatically handle clicks on the Home/Up button, so long
  90.         // as you specify a parent activity in AndroidManifest.xml.
  91.         int id = item.getItemId();
  92.  
  93.         //noinspection SimplifiableIfStatement
  94.         if (id == R.id.signOut) {
  95.             mAuth.signOut();
  96.             finish();
  97.             startActivity(new Intent(getApplicationContext(), Authentication.class));
  98.             return true;
  99.         }
  100.  
  101.         return super.onOptionsItemSelected(item);
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement