I want to create a button, which closes the current activity. Like a "return" button.
Here are code fragments I tried:
Here is the full .java:
public class OtherApps extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_apps);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.otherappsmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.previous:
finish();
break;
case R.id.home:
Context context = getApplicationContext();
CharSequence text = "Activitys are not closed!";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Intent intent = new Intent(this, MainActivity.class);
this.startActivity(intent);
break;
case R.id.exit:
finish();
System.exit(0);
case R.id.help:
String url = "http://www.google.de/";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
return true;
final Button OtherApps = (Button)findViewById(R.id.previousbutton);
OtherApps.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
return true;
}
}
But Eclipse says "the first line is unreachable".
Does anyone know what's the error?
Thanks for help!
If your "first line" is unreachable, then it's more important to know, what the code before these two versions is. It might be, that you have a return statement there or a condition that is always false.
In this case the code for attaching the on-click listeners will never be reached.
p.s.
You have two lines wehre you return from the method what means that the following code is never executed:
switch(item.getItemId()) {
...
default: // here, return if none of the values above matched
return super.onOptionsItemSelected(item);
}
return true; // here, return always
// conclusion: this gets never executed: Eclipse says "line not reachable"
final Button OtherApps = (Button ...
This code should work (the first example should be preferred).
The error you are getting sounds as if you have a return-statement anywhere inside that method BEFORE you have the pasted code. Search for that, it should fix the error.
EDIT:
public class OtherApps extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_apps);
final Button OtherApps = (Button) findViewById(R.id.previousbutton);
OtherApps.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
Related
I'm new in Android programming. I'm working on an application that have multiple activities. I've created a custom menu with ListView. I would like to put this menu in a base activity to be available in all activities. How should I do this?
Till now, I have something like this:
This is for the button to toggle the menu
menuToggelIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Hide layouts if VISIBLE
if(menuLayout.getVisibility() == View.VISIBLE)
{
menuLayout.setVisibility(View.GONE);
}
// Show layouts if they're not VISIBLE
else
{
menuLayout.setVisibility(View.VISIBLE);
}
}
});
And this is for the menu
menuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String name = menuArray[position];
Context context = getApplicationContext();
switch (name) {
case "CASE1":
Intent case1Intent = new Intent(context, Activity1.class);
startActivity(case1Intent);
break;
case "CASE2":
Intent case2Intent = new Intent(context, Activity2.class);
startActivity(case2Intent);
break;
case "CASE3":
Intent case3Intent = new Intent(context, Activity3.class);
startActivity(case3Intent);
break;
case "CASE4":
Intent case4Intent = new Intent(context, Activity4.class);
startActivity(case4Intent);
break;
case "CASE5":
Intent case5Intent = new Intent(context, Activity5.class);
startActivity(case5Intent);
break;
case "CASE6":
Intent case6Intent = new Intent(context, Activity6.class);
startActivity(case6Intent);
break;
case "CASE7":
Intent case7Intent = new Intent(context, Activity7.class);
startActivity(case7Intent);
break;
default:
break;
}
}
});
Android custom menu
make one BaseActivity class and all activity extends by BasyActivity class.
BaseActivity class define your main things that show all the screen like menu and other thing. for example
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.manu_file_name, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.icon) {
Toast.makeText(getApplicationContext(), "Hello World", 0).show();
}
return super.onOptionsItemSelected(item);
}
}
and this activity extends all other activity.
I have a view that has an onClick property:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hey"
android:onClick="showCity" />
Which corresponds to this method:
public void showCity(View view) {
Intent intent = new Intent(this, CityActivity.class);
startActivity(intent);
}
However, I have a menu item that I want to have open the CityActivity as well:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_city:
showCity();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
However, this doesn't work because it's missing the view parameter in the call to showCity() and I'm not sure what it should be in this case.
How do I modify it to work in both cases?
Replace your onOptionsItemSelected code with following following code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_city:
showCity(null);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You showCity must have the View parameter instead of that you can pass null as parameter to your onClick() method.
Since you don't use the view parameter, you can just pass null to it and it will work
Simply pass null i.e. showCity(null)
Write an id attribute to your textview in xml as shown below, you can also remove the onClick attribute of the TextView:
<TextView
android:id="#+id/tShowCity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hey"
/>
Now in your onCreate() method of the activity initize your TextView as shown below and write the click listener for the TextView as shown below:
TextView tv = (TextView)findViewById(R.id.tShowCity);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showCity();
}
});
now define your show city method as shown below:
public void showCity() {
Intent intent = new Intent(this, CityActivity.class);
startActivity(intent);
}
Now the above method can also be used in your Menu and will function without any issue as:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_city:
showCity();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
IF you are not using view , so why are you add in your Method Parameter...
public void showCity(View view) {
Intent intent = new Intent(this, CityActivity.class);
startActivity(intent);
}
Look...
public void showCity() {
Intent intent = new Intent(this, CityActivity.class);
startActivity(intent);
}
Simple..... :)
If you do not want to use View property ..as answer already you can pass null value to your showCity()
showCity(null);
But if you want to use specific view properties in your showCity() you can pass view a with id by findViewById() method..like.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
showCity(this.findViewById(R.id.textview1));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I want to be able to touch the items in the main activity and once touch the item will save to the favorites class. I'm assuming I need to implement and OnItemClickListener but I am having trouble implementing it correctly. please help
Also do I need to add another intent code and place the onclicklistener inside of there? If more information is needed please let me know, I have been stuck on this issue for awhile now. Thank you
This is my main activity code
import com.parse.ParseQueryAdapter;
public class WingmanListActivity extends ListActivity {
private ParseQueryAdapter<Tip> mainAdapter;
private FavoriteTipAdapter favoritesAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getListView().setClickable(true);
mainAdapter = new ParseQueryAdapter<Tip>(this, Tip.class);
mainAdapter.setTextKey("title");
mainAdapter.setImageKey("photo");
//Subclass ParseQueryAdapter
favoritesAdapter = new FavoriteTipAdapter(this);
//Default view is all wingman tips
setListAdapter(mainAdapter);
/* String url = "http://twitter.com/";
WebView view = (WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(url); */
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_wingman_list, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh: {
updateTipList();
break;
}
case R.id.action_favorites: {
showFavorites();
break;
}
case R.id.action_new: {
newTip();
break;
}
case R.id.twitter: {
twitter();
break;
}
}
return super.onOptionsItemSelected(item);
}
private void twitter() {
Intent a = new Intent(this, Twitter.class);
startActivityForResult(a, 0);
}
private void updateTipList() {
mainAdapter.loadObjects();
setListAdapter(mainAdapter);
}
private void showFavorites() {
favoritesAdapter.loadObjects();
setListAdapter(favoritesAdapter);
}
private void newTip() {
Intent i = new Intent(this, NewTipActivity.class);
startActivityForResult(i, 0);
I have an activity that can be asked to run after clicking buttons on many different activities and hence it does not have a "single parent". Therefore in the android manifest I cannot define its parent so I cant get the "Up" button to function properly.
Is there a way I can have the "up" button return to the activity that called it?
You can pass ComponentName of starting activity as an extra
intent = new Intent(this, UpButtonActivity.class);
intent.putExtra(EXTRA_PARENT_COMPONENT_NAME, new ComponentName(this, ThisActivity.class));
startActivity(intent);
The Activity with up button
private ComponentName parent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
parent = getIntent().getParcelable(EXTRA_PARENT_COMPONENT_NAME);
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getId()) {
case android.R.id.home:
if (parent != null) {
final Intent parentIntent = new Intent();
parentIntent.setComponentName(parent);
startActivity(parentIntent);
finish();
return true;
} else {
return super.onMenuItemSelected(featureId, item);
}
//...
}
}
I have one MainActivity and I have defined below code in onCreate() method. The intention is, when MainActivity gets extra String "EXIT" then show Toast message:
Intent current = getIntent();
if (current !=null && current.getStringExtra("EXIT") != null) {
Toast.makeText(this, "exiting", Toast.LENGTH_LONG).show();
}
This MainActivity starts another activity "DayOne" on some button press like:
public void processGo(View v){
Intent i = new Intent(MainActivity.this,DayOne.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
MainActivity.this.startActivity(i);
}
Now I am returning back from "DayOne" to MainActivity after putting extra string "EXIT". This I am doing inside onOptionsItemSelected(MenuItem item) method:
public boolean onOptionsItemSelected(MenuItem item){
if(item.getTitle().equals("Exit")){
Intent i = new Intent(DayOne.this,MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("EXIT", "EXIT");
startActivity(i);
finish();
}
return super.onOptionsItemSelected(item);
}
The issue is, when MainActivity is getting called from DayOne with extra string "EXIT"; I am not seeing the Toast message defined in MainActivity. What is missing or wrong here?
Appreciate any help.
Thanks all for your comments and helps.
I have figured out the issue here. It was because the manifest file had entry an entry android:launchMode="singleInstance" for Both the activities (MainActivity and DayOne Activity)..
Removing it from there, it worked fine.
First check your null then get the Intent String, can you just make your code look like below within in onCreate of MainActivity#
Bundle extra= getIntent().getExtras();
if(extra!=null){
String _StrExit=extra.getString("EXIT");
if(_StrExit.equalsIgnoreCase("EXIT")){
Toast.makeText(this, "exiting", Toast.LENGTH_LONG).show();
}
}
Update
Make change while calling Intent from menuitem
public boolean onOptionsItemSelected(MenuItem item){
if(item.getTitle().equals("Exit")){
Intent i = new Intent(DayOne.this,MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
String _Str="EXIT";
i.putExtra("EXIT", _Str);
startActivity(i);
finish();
}
return super.onOptionsItemSelected(item);
}
Its working for me.
MainActivity:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent current = getIntent();
if (current != null && current.getStringExtra("EXIT") != null) {
Toast.makeText(this, "exiting", Toast.LENGTH_LONG).show();
}
}
public void processGo(View view) {
Intent i = new Intent(MainActivity.this, OneDayActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
}
OneDayActivity:
public class OneDayActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_day_one);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.day_one, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getTitle().equals("Exit")) {
Intent i = new Intent(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("EXIT", "EXIT");
startActivity(i);
finish();
}
return super.onOptionsItemSelected(item);
}
}
dont clear the stack instead of if you want to do some task when you come back to your main activity (using "EXIT" string) you can achieve that using onActivityResult also.:
public void processGo(View v){
Intent i = new Intent(MainActivity.this,DayOne.class);
MainActivity.this.startActivity(i,10); // 10 is the id to handle
}
#Override
public void onActivityResult( int requestCode, int resultCode, Intent data )
{
super.onActivityResult( requestCode, resultCode, data );
switch ( requestCode )
{
case ( 10 ): // id that we pass on start activity
{
if ( resultCode == Activity.RESULT_OK )
{
Toast.makeText(this, "exiting"+data.getStringExtra( "EXIT", "" ), Toast.LENGTH_LONG).show();
}
}
break;
}
}
On finish
public boolean onOptionsItemSelected(MenuItem item){
if(item.getTitle().equals("Exit")){
Intent resultIntent = new Intent();
resultIntent.putExtra( "EXIT", "EXIT" );
setResult( Activity.RESULT_OK, resultIntent );
finish();
}
return super.onOptionsItemSelected(item);
}
on finish it will go back to Main Activity and call onActivityResult automatically where you can do your task.
Sorry for the typo hope it will help.