Android : How do I rewrite this so that I only use one activity?

on Monday, July 7, 2014


I have two different activities, and each one is a different page of the app. But I want to put everything into one activity. I also noticed that a lot of the code in these two activities are similar or exactly the same.


But the problem is, when I put in the new code from DisplayMessageActivity, I just get led back to the first page when I click "NEXT". (In simpler words, when I click "NEXT", I just go back to the page that I made in MainActivity) Any help is appreciated!


Here is my MainActivity:



package com.example.myfirstapp;

import android.app.Activity;
import android.app.Fragment;
import android.graphics.Typeface;
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.content.Intent;
import android.widget.EditText;
import android.widget.TextView;
//import android.widget.TextView;


public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.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.main, 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 PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}

/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}


And here is my DisplayMessageActivity:



package com.example.myfirstapp;

import android.app.Activity;
import android.app.ActionBar;
import android.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.content.Intent;
import android.widget.TextView;
import android.graphics.Typeface;
import android.os.Build;

import com.example.myfirstapp.R;

public class DisplayMessageActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_display_message);

// Get the message from the intent
Intent intent = getIntent();
String message = "Hi " + intent.getStringExtra(MainActivity.EXTRA_MESSAGE) + "!";

// Create the text view
// TextView textView = new TextView(this);
// textView.setTextSize(25);
// textView.setText(message);
TextView textView1 = (TextView) findViewById(R.id.text1);
TextView textView2 = (TextView) findViewById(R.id.text2);


textView1.setText(message);
textView1.setTextSize(35);
textView2.setTextSize(30);


// Font path
String fontPath = "fonts/Sketchy.ttf";

// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);

// Applying font
textView1.setTypeface(tf);
textView2.setTypeface(tf);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, 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 PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_display_message, container, false);
return rootView;
}
}

public void sendMessage(View view) {
Intent intent = new Intent(this, FlashlightActivity.class);
startActivity(intent);
}

}

0 comments:

Post a Comment