onActivityResult wasn't called when click home button - android

I have three activity like this :
+ LoginPage : display only one time when app installed
+ MainActivity : Home screen of app
+ TextNoteActivity: Sub activity is called by startActivityForResult from MainActivity
I don't know why when i click up home button from TextNoteActivity. The app will close.
The following is my code.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lilprogramming.bossnote">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name="activity.MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"
>
</activity>
<activity
android:name="activity.LoginPage"
android:label="#string/app_name"
android:noHistory="true"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="activity.TextNoteActivity"
android:parentActivityName="activity.MainActivity"
>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="activity.MainActivity" />
</activity>
<activity
android:name="activity.AddTagActivity"
android:label="#string/title_activity_add_tag"
android:theme="#style/AppTheme">
</activity>
</application>
In MainActivity :
Intent i = new Intent(this, TextNoteActivity.class);
startActivityForResult(i, REQUEST_ADDTEXTNOTE);
In TextNoteActivity :
toolbar = (Toolbar) findViewById(R.id.addTextNote_toolbar);
toolbar.setTitle("");
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addTextNote();
Toast.makeText(TextNoteActivity.this, "Back then", Toast.LENGTH_SHORT).show();
finish();
}
});
Update
private void addTextNote() {
String title = etTitle.getText().toString();
String content = etContent.getText().toString();
if (!title.isEmpty() && !content.isEmpty()) {
TextNote textNote = new TextNote(title, 0, content);
database.addTextNote(textNote, null);
setResult(RESULT_OK);
}
else {
setResult(Activity.RESULT_CANCELED);
}
}

That is expected behavior
There is no reason for your activity to return a result when the user pushes the Home button.
If you need to catch the Home button event
You could override onPause() and finish() the activity.
DISCLAIMER
But I'm afraid this is not a good app-behavior
Because the user won't expect the app state to change when he pushes the Home button.

Related

app unfortunately stopped when use "Intent intent = new ...." method to change screen

I have created a simple app.
one screen of my app has following code and I use a Button (id "sendManual") to change the app view to another one.
please look at the end of this code, you can seen the button id "sendManual".
I have setup it to change screen view to App2Activity.java
(I have put the Code of App2Activity java below this code, then you can check it also)
I installed this app on my android phone, But when I click Button(id"sendManual") app suddenly Stopped and says
Unfortunately,XXXXX app has stopped
public class showPermission implements View.OnClickListener {
Dialog dialog;
Activity activity;
public void showDialog(final Activity activity){
dialog = new Dialog(activity);
dialog.setCancelable(false);
this.activity=activity;
dialog.setTitle("XXXXX");
dialog.setContentView(R.layout.permission);
Button Sendcancel = (Button) dialog.findViewById(R.id.sendCancel);
Button SendNow = (Button) dialog.findViewById(R.id.sendNow);
Button sendManual = (Button) dialog.findViewById(R.id.sendManual);
sendManual.setOnClickListener(this);
SendNow.setOnClickListener(this);
Sendcancel.setOnClickListener(this);
dialog.show();
}
#Override
public void onClick(View view) {
int id=view.getId();
if(id==R.id.sendCancel){
dialog.dismiss();
}
else if(id==R.id.sendNow){
sendSMS();
}
else if(id==R.id.sendManual){
Intent intent = new Intent(activity, App2Activity.class);
activity.startActivity(intent);
}
}
Here is the code of App2Activity.java
public class App2Activity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
sendSMS2();
}
});
}
............ etc
Here is manifest file.
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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>
</application>
You are not properly set context in Intent.Use this
Intent intent = new Intent(getApplicationContext(), App2Activity.class);
startActivity(intent);
instead of
Intent intent = new Intent(activity, App2Activity.class);
activity.startActivity(intent);
This all is happening because you didn't declare App2Activity in Manifest.xml. That is compulsory to declare all the activities in manifest file.
Replace your manifest.xml with this one:
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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=".App2Activity"/>
</application>

Why my app is creased when i press a back button of actionBar in android?

Explanation:
I have mainActivity in which i implemented a navigationDrawer. There are multiple fragment.In first,fragment namely HomeFragment. In homeFragment i am using viewpager and listview. in my viewpager there is two things first, if match is started then it's goes to summaryCard activity. second if match no started it's goes to notstartedmatchdetail activiy.
when match is starting first i goes to summarycard activity.in summarycard activity i put the button. when i press the button in summarycard it's directly goes to fullscorecard.e.g. homeFragment->summarycard->fullscorecard.
In listview, it seen the detail of the match which was completed.Here i am directly goes to fullscorecard.e.g. homefragment->fullscorecard.
Situation:
While i goes to homefragment->fullscorecard
After pressing back button of fullscorecard it goes to summarycard instead of homefragment.
Here is my homeFragment where i called an adapter
LiveAdapter adapterTabRecent = new LiveAdapter(getContext(), lst_obj, PagerLength, list_status, lst_key);
adapterTabRecent.notifyDataSetChanged();
pagerRecentMatches.setAdapter(adapterTabRecent);
pagerRecentMatches.setCurrentItem(current_pos);
ScheduleAdapter CmAdapter = new ScheduleAdapter(getContext(), arr_completed, lst_key2);
CmAdapter.notifyDataSetChanged();
completed_listview.setAdapter(CmAdapter);
Above both the adapter called in homefragment for started match it called liveAdapter and for completed match is called ScheduleAdapter.
Here, is my live adapter
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String key=lst_key.get(position);
String status_value=list_status.get(position);
if(status_value.equals("notstarted")){
Log.e("MATCH_KEY",""+key);
Intent intent=new Intent(context,NotStartedMatchDetails.class);
intent.putExtra("mainobj", lst.get(position).toString());
context.startActivity(intent);
}
if(status_value.equals("started")){
Log.e("MATCH_KEY",""+key);
MainActivity mainActivity=(MainActivity)context;
Intent intent=new Intent(context,SummaryCard.class);
intent.putExtra("Key", key);
intent.putExtra("access_token",mainActivity.getMyData());
context.startActivity(intent);
}
}
});
Here is a code for listview where match is completed.
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
match_key = arr_matchKey.get(position);
Log.e("Schedule complted key", match_key);
MainActivity activity = (MainActivity) context;
GetValue gv = new GetValue(context);
gv.setFullUrl(match_key);
gv.setAccessToken(activity.getMyData());
gv.execute();
}
});
Here is my manifiest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.angelnx.cricvilla.cricvilla">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/MyMaterialTheme">
<activity
android:name=".SplashScreen"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".NotStartedMatchDetails"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity">
</meta-data>
</activity>
<activity
android:name=".SummaryCard"
android:screenOrientation="portrait"
android:theme="#style/MyMaterialTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".ScoreCard"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".SummaryCard" />
</activity>
<activity
android:name=".PlayerInfoDetails"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".FullScore"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".PlayerIccStats"
android:screenOrientation="portrait">
</activity>
</application>
</manifest>
Consider fullscorecard as Scorecard in manifiest.
This problem is cause only in ice-cream sandwich lg model.
Otherwise it' working correctly in another devices.
Please help me to solve this problem.

How to get a login activity before mainactivity

is it possible to get a login activity in between splash and main activity in android studio.`it should be after splash activity and before main.how to get it.
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Register"
android:label="#string/title_activity_register"
android:theme="#style/AppTheme.NoActionBar"/>
<activity android:name=".MainActivity"
android:theme="#style/AppTheme.NoActionBar"/>
</application>
You may change your manifest like this
<application
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:logo="#drawable/icon"
>
<activity
android:name=".SplashActivity"
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=".LoginPage"></activity>
<activity android:name=".MainPage"></activity>
</application>
and make sure your intents are appropriare in order
When you call splash activity, inside splash activity just call loginactivity and if you are on login activity, on press of the button, call mainactivity. That's it.
public class SplashActivity extends Activity {
private final int STR_SPLASH_TIME = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
startSplashTimer();
}
private void startSplashTimer() {
try {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
}, STR_SPLASH_TIME);
} catch (Exception e) {
e.printStackTrace();
}
}
From SplashActivity, it will be going to the LoginActivity, when this happened after that onclick of the button call MainActivity.
So your flow should be like this,
SplashActivity --> LoginActivity --> MainActivity
I've answered a similar question two or three weeks ago. Find the answer here.
However, in my opinion, creating an activity just for showing a splash is not so good. Essentially, your app should have as few activities as it can. Activities are, with no doubts, the main and heaviest component for an app. So, for just showing a splash for you may want to show that splash in your LoginActivity until further resources are loaded. Once all are loaded, you can hide that splash and show log-in form.
Do something like this:
SplashActivity.java
public class SplashActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_activity);
...
if(!alreadyLoggedIn()) {
Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
startActivity(intent);
} else {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
}
}
}
LoginActivity.java
public class LoginActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_activity);
if(loginSuccessful()) {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
} else {
// show ERROR
}
}
}

Hide Action Bar for Splash Screen

I'm trying to create splash screen without action bar.
Firstly before created splash screen, action bar in main activity and when I create splash screen, action bar comes to splash screen and main activity is full screen. I searched method like getwindow(), getActionBar(), but when I use these method program says to me unfortunately stopped. So what I'm missing?
How can I avoid actionBar in splash screen?
My code:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_item);
Thread th=new Thread(){
public void run(){
try {
sleep(4000);
Intent intent=new Intent(MainActivity.this,SplashScreen.class);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
finally{
finish();
}
}
};
th.start();
}
MANÄ°FEST:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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=".SplashScreen"
android:label="#string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.SPLASHSCREEN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
First, import the support library in the top of your app:
import android.support.v7.app.ActionBarActivity;
and change your code as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_item);
getSupportActionBar().hide();
}
Using AppCompat for being supported for all versions:
<activity
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Put that before your setContentView(...), should get the job done.
For Android 3.0 or higher use ActionBarAPI#hide
For lower versions you will need to use Android Support Library.
Use ActionBar as
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
Ref docs
Also if your requirement in static, then you can choose a theme for your activity that dos not have actionbar such as
android:theme="#android:style/Theme.Holo.Light.NoActionBar"
You can do this as:
<activity android:theme="#android:style/Theme.Holo.Light.NoActionBar"
android:name=".name_here"
android:label="#string/app_name" >
use a style without actionbar, also in your splash screen activity java extend Activity and make the splash screen your MAIN activity and in this activity you call an Intent to open your MainActivity after some seconds
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
</activity>
<activity android:theme="#android:style/Theme.Holo.Light.NoActionBar"
android:name=".SplashScreen"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Example:
In your SplashScreen activity write this code. This will open your MainActivity after 2 seconds.
new Handler().postDelayed(new Runnable()
{
public void run()
{
Intent localIntent = new Intent(SplashScreen.this, MainActivity.class);
SplashScreen.this.startActivity(localIntent);
SplashScreen.this.finish();
}
}, 2000L);
This is the simplest method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
}
where R.layout.activity_main is replaced by your layout activity e.g. activity_splash

Nothing happens on startActivity()

I have 2 activities with a few buttons etc. I want to start new activity. I have done everything the tutorial said, but the 2nd activity does not start!
case R.id.btnIstorija:
Intent i = new Intent (this,KlasaPrikazBaze.class);
startActivity(i);
break;
It should start my 2nd activity
public class KlasaPrikazBaze extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.prikazbaze);
TextView TV = (TextView)findViewById(R.id.tvSQLinfo);
KlasaBaze info = new KlasaBaze(this);
info.open();
String podatak = info.DohvatiPodatak();
info.close();
}
}
However nothing happens.
Manifest:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".TalentiFinalActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".KlasaPrikazBaze"></activity>
</application>

Categories

Resources