New Intent doesn't open on button click - android

Blockquote
I try to change the display by simply clicking the button. I guess I should use Intent for doing this but no any effect, please help me :(
here is my code:
FYP.JAVA
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button ProfileTemplate = (Button)findViewById(R.id.ProfileTemplate);
ProfileTemplate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent mIntent = new Intent(FYP.this, ProfileTemplate.class);
startActivity(mIntent);
finish();
}
});
public class ProfileTemplate extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profiletemplate);
}
}

Below is complete project with 2 Activities. One has button, when you click it, it starts the other Activity.
More on Buttons and Clicking.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Hello, sup son!" />
<Button android:id="#+id/btnButton" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:layout_gravity="left"
android:onClick="changeActivity" android:text="changeActivity" />
</LinearLayout>
main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="New Activity Started" />
</LinearLayout>
class Hello.java
public class Hello extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
//default button handler
public void changeActivity(View view) {
startActivity(new Intent(this, NewActivity.class));
}
}
class NewActivity.java
public class NewActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.stackoverflow.example"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="New App Name">
<activity android:name=".Hello"
android:label="New App Name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NewActivity" android:label="New App Name"></activity>
</application>
</manifest>

Related

Starting activity from fragment button click (intent) throws blank screen

I'm trying to start an activity from a button which is placed in a fragment, but when I run the app (in emulator and in a real device) and I press that button, the activity doesn't start and a blank screen whithout navigation bar appears. I've read a lot about this, and the most common error people had is that they didn't declare the Activity or they weren't inflating the correct layout. Any idea would be appreciated, because I don't now what to do right now.
Here is the code of the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.isa.example" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/Theme.CustomMaterial" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NewActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/Theme.CustomMaterial" >
<intent-filter>
<action android:name="com.isa.example.NewActivity" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here is the code for the fragment layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start new activity"/>
</RelativeLayout>
Code for the fragment:
public class Fragment1 extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getActivity(), NewActivity.class);
startActivity(i);
}
});
return view;
}
}
Code for the new activity layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="This is new activity"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
</LinearLayout>
And code for the new activity:
public class NewActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.newactivity_layout);
}
}
What is your minimum sdk version? Your version of onCreate is introduced in 21: stackoverflow.com/a/29871359/1541763 Remove the PersistableBundle persistentState from your parameters

How to show menu item in android application on click

I have to make a list menu item in android that contains three menu items in a list view i want to show some content on click of each menu.Here is a screen of which i have done so far..
This is the menu i have created .i want that i will click on Menu1 and the Menu1 will show in a page..i am posting my code..
public class MainActivity extends ListActivity {
String[] menus={"Menu1","Menu2","Menu3"};
#Override
protected void onListItemClick(ListView l,View v,int position,long id){
super.onListItemClick(l,v,position,id);
String name=menus[position];
try{
Class main=Class.forName("com.betacoading.createlistmenu.app"+name);
Intent intent=new Intent(MainActivity.this,main);
startActivity(intent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
setListAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_expandable_list_item_1,menus));
}
#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);
}
}
public class Menu_1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_1);
Button back=(Button)findViewById(R.id.button);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent in=new Intent(getApplicationContext(),MainActivity.class);
startActivity(in);
}
});
}
}
public class Menu_2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_2);
Button back=(Button)findViewById(R.id.button);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent in=new Intent(getApplicationContext(),MainActivity.class);
startActivity(in);
}
});
}
}
public class Menu_3 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_3);
Button back=(Button)findViewById(R.id.button);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent in=new Intent(getApplicationContext(),MainActivity.class);
startActivity(in);
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.betacoading.createlistmenu.app" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.betacoading.createlistmenu.app.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Menu_1"
android:label="#string/app_name" >
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Menu_2"
android:label="#string/app_name" >
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Menu_3"
android:label="#string/app_name" >
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Menu_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Menu 1"
android:textSize="24dp"
android:layout_gravity="center_horizontal"
/>
<Button
android:id="#+id/button"
android:layout_width="200dp"
android:layout_height="400dp"
android:text="Back"
android:textSize="16dp"
/>
</LinearLayout>
Menu2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Menu 2"
android:textSize="24dp"
android:layout_gravity="center_horizontal"
/>
<Button
android:id="#+id/button"
android:layout_width="200dp"
android:layout_height="400dp"
android:text="Back"
android:textSize="16dp"
/>
</LinearLayout>
menu3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Menu 3"
android:textSize="24dp"
android:layout_gravity="center_horizontal"
/>
<Button
android:id="#+id/button"
android:layout_width="200dp"
android:layout_height="400dp"
android:text="Back"
android:textSize="16dp"
/>
This is my code i have tried so far..please help me to make it dynamic
Try this link
http://www.androidviews.net/2013/04/sliding-layer/
This will help you to make sliding menu from bottom, left, right, and top with lots of animation.
You can put anything inside it and easily work on it in you java class.
Hope it will help.

FragmentActivity Buttons to switch between Fragments

All I want to do is a simple Button page switcher between the MainActivity and ArticleGroups pages. I will be implementing a paging class afterwards but I want to get the buttons working first.
Once I run the Application the app will crash and force close so I am certainly doing something wrong.
MainActivity.java
public class MainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button)findViewById(R.id.articlebtn);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent i = new Intent(MainActivity.this, ArticleGroup.class);
startActivity(i);
}
});
}
#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;
}
}
activity_main.xml
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity">
<fragment
android:id="#+id/activitymain_fragment"
android:name="com.dharris.mindfulemployer_v4.MainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/articlebtn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#color/aqua"
android:onClick="onClick" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/articlebtn"
android:layout_alignBottom="#+id/articlebtn"
android:layout_toRightOf="#+id/articlebtn"
android:text="#string/hello_world" />
</RelativeLayout>
ArticleGroup.java
public class ArticleGroup extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.articlegroup);
Button b = (Button) findViewById(R.id.articlebtn);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(ArticleGroup.this, MainActivity.class);
startActivity(i);
}
});
}
}
articlegroup.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="#+id/articlegroupfragment"
android:name="com.dharris.mindfulemployer_v4.ArticleGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/articlebtn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#color/aqua"
android:onClick="onClick" />
</RelativeLayout>
Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dharris.mindfulemployer_v4"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<!-- Activity Default -->
<activity
android:name="com.dharris.mindfulemployer_v4.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Activity ArticleGroup -->
<activity
android:name=".ArticleGroup"
android:label="#string/app_name"
></activity>
</application>
</manifest>
If anyone can make sense as to why this is not working then I will be very appreciative.
You have
android:onClick="onClick" />
You need to have a method by name onClick in Activity which takes view as a param
You can remove this
Button b = (Button)findViewById(R.id.articlebtn);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent i = new Intent(MainActivity.this, ArticleGroup.class);
startActivity(i);
}
});
And have
public void onClick(View V)
{
// do something on button click
}
Delete this line from activity_main.xml and articlegroup.xml
android:onClick="onClick"

android app one page to another page not working

Here I done code for 5 buttons like Aboutus,development,carriers,services,contactus.
Here I clicked about us button that shows errors as unfortunately app closed.
Here is my code :
MainActivity.java
public class MainActivity extends Activity {
Button aboutus,development,service,carriers,contactus;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button aboutus=(Button) findViewById(R.id.aboutus);
aboutus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(v.getContext(),aboutus.class);
startActivity(i);
}
});
}
#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;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/aboutus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="About us"
/>
<Button
android:id="#+id/services"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Services"
/>
<Button
android:id="#+id/development"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Development"
/>
<Button
android:id="#+id/carriers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Carriers"
/>
<Button
android:id="#+id/contactus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Contact Us"
/>
</LinearLayout>
</RelativeLayout>
aboutus.java
public class aboutus extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aboutus);
}
}
aboutus.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Aboutus Page"/>
</LinearLayout>
AndroidManifest.xml is this correct ?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.globalinfosoft"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.globalinfosoft.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<Activity android:name= "aboutus"/>
</application>
</manifest>
MainActivity.java
package com.example.a;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button aboutus, development, service, carriers, contactus;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button aboutus = (Button) findViewById(R.id.aboutus);
aboutus.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Starting a new Intent
Intent nextScreen = new Intent(getApplicationContext(),aboutus.class);
startActivity(nextScreen);
}
});
}
}
Here is Androidmanifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.a.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.a.aboutus"></activity>
</application>
</manifest>
Instead of v.getContext() use 'getApplicationContext()'.
Intent i = new Intent(getApplicationContext(),aboutus.class);
startActivity(i);
Try this ..
Button aboutus=(Button) findViewById(R.id.aboutus);
aboutus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),aboutus.class);
startActivity(i);
}
});
First, you should make sure that you have declared the aboutus acitivity in your AndroidManifest.xml file.
<Activity android:name= "aboutus"/>
Second, you can use
Intent i = new Intent(getApplicationContext(),aboutus.class);
or
Intent i = new Intent(MainActivity.this,aboutus.class);
instead of
Intent i = new Intent(v.getContext(),aboutus.class);
Intent i = new Intent(v.getContext(),aboutus.class);
startActivity(i);
instead of code use following code and instruction:
Intent i = new Intent(MainActivity.this,aboutus.class);
startActivity(i);
you have to declare the About us activity on AndroidManifest.xml like this
<Activity android:name= ".aboutus"/>

Android fragments setRetainInstance(true) not works (Android support library)

I have a problem with saving fragment state. I try to use setRetainInstance, but cant make it work((( I change a state to 2 using button1, but after changing screen orientation i see 1 when pressing on button2. Where is my mistake?
public class TestFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
setRetainInstance(true);
super.onCreate(savedInstanceState);
}
private String state = "1";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, container, false);
//button for changing state
((Button)view.findViewById(R.id.button1)).setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
state = "2";
}
});
//button for showing state
((Button)view.findViewById(R.id.button2)).setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Toast.makeText(getActivity(), state, Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
EDIT
Activity:
public class MainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Activity layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:name="ru.ee.TestFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
Fragment layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change to 2" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show" />
</LinearLayout>
Manifest XML:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ru.ee.testfrag"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="ru.ee.MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Your fragment will only get re-used if you give the fragment an Id in the layout, change
<fragment
android:name="ru.ee.TestFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
to be
<fragment
android:id="#+id/a_fragment"
android:name="ru.ee.TestFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
There isn't anything wrong with the code you posted. If you have the fragment implemented correctly, it should be giving you a state of 2. Maybe you could post your activity and xml files. There error must be somewhere else.

Categories

Resources